-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintarithmetic.py
292 lines (252 loc) · 10.4 KB
/
intarithmetic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Kevin Kokomani
# Python 2.7
# Using the arithmetic functions in big.py, this program performs integer division for two positive integers represented
# as bytearrays of the same size. The division function works correctly with negative numbers, bytearrays of different sizes,
# and the program also supports the modulo operation and modular exponentiation. All code in big.py supplied by the instructor,
# all code in intarithmetic.py written by Kevin Kokomani
import unittest
import big
#Main divide function
def divide(a, b):
if len(a) < len(b):
a = big.sign_extend(a, len(b)-len(a))
if len(b) < len(a):
b = big.sign_extend(b, len(a) - len(b))
if big.is_negative(a) and big.is_negative(b):
quotient, remainder = dividePositive(big.negate(a), big.negate(b))
return quotient
elif big.is_negative(a):
quotient, remainder = dividePositive(big.negate(a), b)
return divideDifferentSigns(quotient, remainder)
elif big.is_negative(b):
quotient, remainder = dividePositive(a, big.negate(b))
return divideDifferentSigns(quotient, remainder)
else:
quotient, remainder = dividePositive(a, b)
return quotient
#Main modulo function
def modulo(a, b):
if len(a) < len(b):
a = big.sign_extend(a, len(b) - len(a))
if len(b) < len(a):
b = big.sign_extend(b, len(a) - len(b))
if big.is_negative(a) and big.is_negative(b):
quotient, remainder = dividePositive(big.negate(a), big.negate(b))
return big.negate(remainder)
elif big.is_negative(a):
quotient, remainder = dividePositive(b, big.negate(a))
return remainder
elif big.is_negative(b):
quotient, remainder = dividePositive(big.negate(b), a)
return big.negate(remainder)
else:
quotient, remainder = dividePositive(a, b)
return remainder
#Main Modulo Exp Function
def moduloExp(a, b, n):
zero = bytearray(len(b))
if (big.equal(b, zero)):
return big.int2bytearray(1, len(b))
z = moduloExp(a, big.half(b), n)
if (big.is_even(b)):
return modulo(big.multiply(z, z), n)
else:
return modulo(big.multiply(a, big.multiply(z, z)), n)
#Helper functions
def dividePositive(a, b):
zero = bytearray(len(a))
if big.equal(a, zero):
return (zero,zero)
quotient, remainder = dividePositive(big.half(a),b)
quotient = big.twice(quotient)
remainder = big.twice(remainder)
if (not big.is_even(a)):
remainder = incrementByOne(remainder)
if big.greater_than(remainder, b) or big.equal(remainder, b):
remainder = big.add(remainder, big.negate(b))
quotient = incrementByOne(quotient)
return (quotient, remainder)
def divideDifferentSigns(a, b):
if not big.equal(b, bytearray(len(b))):
return big.negate(incrementByOne(a))
else:
return big.negate(a)
def incrementByOne(number):
return big.add(number, big.int2bytearray(1, len(number)))
class IntArithmetic(unittest.TestCase):
# START Unit Tests for Problem 1
def testDividePositive1(self):
print("Positive Division Test 1:")
a = big.int2bytearray(120, 8)
b = big.int2bytearray(4, 8)
c = divide(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, 120/4)
def testDividePositive2(self):
print("Positive Division Test 2:")
a = big.int2bytearray(47, 8)
b = big.int2bytearray(5, 8)
c = divide(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, 47/5)
# END Unit Tests for Problem 1
# START Unit Tests for Problem 2
def testDivideNegative1(self):
print("Negative Division Test 1:")
a = big.int2bytearray(-5, 8)
b = big.int2bytearray(4, 8)
c = divide(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, -5/4)
def testDivideNegative2(self):
print("Negative Division Test 2:")
a = big.int2bytearray(8, 8)
b = big.int2bytearray(-4, 8)
c = divide(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, 8/-4)
def testDivideNegative3(self):
print("Negative Division Test 3:")
a = big.int2bytearray(-6, 8)
b = big.int2bytearray(3, 8)
c = divide(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, -6/3)
def testDivideNegative4(self):
print("Negative Division Test 4:")
a = big.int2bytearray(-15, 8)
b = big.int2bytearray(-5, 8)
c = divide(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, -15/-5)
# END Unit Tests for Problem 2
# START Unit Tests for Problem 3
def testDivideSignExt1(self):
print("Sign Extension Division Test 1:")
a = big.int2bytearray(1234567890)
b = big.int2bytearray(2)
c = divide(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, 1234567890/2)
# END Unit Tests for Problem 3
# START Unit Tests for Problem 4
def testModulo1(self):
print("Modulo Test 1:")
a = big.int2bytearray(7, 8)
b = big.int2bytearray(3, 8)
c = modulo(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "%" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "%", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, 7 % 3)
def testModulo2(self):
print("Modulo Test 2:")
a = big.int2bytearray(-98, 8)
b = big.int2bytearray(-17, 8)
c = modulo(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "%" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "%", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, -98 % -17)
# END Unit Tests for Problem 4
# START Unit Tests for Problem 5
def testModulo3(self):
print("Modulo Test 3:")
a = big.int2bytearray(-3, 8)
b = big.int2bytearray(4, 8)
c = modulo(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "%" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "%", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, -3%4)
def testModulo4(self):
print("Modulo Test 4:")
a = big.int2bytearray(3, 8)
b = big.int2bytearray(-4, 8)
c = modulo(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "%" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "%", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, 3%-4)
def testModulo5(self):
print("Modulo Test 5:")
a = big.int2bytearray(1234567890)
b = big.int2bytearray(2)
c = modulo(a, b)
ans = big.bytearray2int(c)
printstatement = "\n" + str(big.bytearray2int(a)) + "%" + str(big.bytearray2int(b)) + "=" + str(ans)
print(printstatement)
# print("\n", big.bytearray2int(a), "%", big.bytearray2int(b), "=", ans)
self.assertEqual(ans, 1234567890%2)
# END Unit Tests for Problem 5
# START Unit Tests for Problem 6
def testExpMod1(self):
print("Exponential Modulo Test 1:")
a = 5
aArray = big.int2bytearray(5, 8)
b = 756
bArray = big.int2bytearray(756, 8)
n = 25
nArray = big.int2bytearray(25, 8)
c = moduloExp(aArray, bArray, nArray)
ans = big.bytearray2int(c)
printstatement = "\n" + str(a) + "^" + str(b) + "%" + str(n) + "=" + str(ans)
print(printstatement)
# print("\n", a, "^", b, "%", n, "=", ans)
self.assertEqual(ans, pow(a, b, n))
def testExpMod2(self):
print("Exponential Modulo Test 2:")
a = 9
aArray = big.int2bytearray(9, 8)
b = 3
bArray = big.int2bytearray(3, 8)
n = 4
nArray = big.int2bytearray(4, 8)
c = moduloExp(aArray, bArray, nArray)
ans = big.bytearray2int(c)
printstatement = "\n" + str(a) + "^" + str(b) + "%" + str(n) + "=" + str(ans)
print(printstatement)
# print("\n", a, "^", b, "%", n, "=", ans)
self.assertEqual(ans, pow(a, b, n))
def testExpMod3(self):
print("Exponential Modulo Test 3:")
a = 12
aArray = big.int2bytearray(12, 8)
b = 17
bArray = big.int2bytearray(17, 8)
n = 7
nArray = big.int2bytearray(7, 8)
c = moduloExp(aArray, bArray, nArray)
ans = big.bytearray2int(c)
printstatement = "\n" + str(a) + "^" + str(b) + "%" + str(n) + "=" + str(ans)
print(printstatement)
# print("\n", a, "^", b, "%", n, "=", ans)
self.assertEqual(ans, pow(a, b, n))
# END Unit Tests for Problem 6
if __name__ == '__main__':
unittest.main()