-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiv.py
More file actions
33 lines (32 loc) · 738 Bytes
/
div.py
File metadata and controls
33 lines (32 loc) · 738 Bytes
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
def div_loop(d, r):
'''
Accessing the diviend and divisor by parameter D and R.
>>> a, b = div_loop(11, 3)
>>> a
3
>>> b
2
'''
temp = r
while d > (r << 1):
r <<= 1
q = 0
while r >= temp: # while R can still represent one or more divisors
q <<= 1
if d >= r:
d -= r
q += 1
r >>= 1
if d >= temp:
q += 1
d -= temp
return q, d
if __name__ == "__main__":
import numpy as np
for i in range(100):
a, b = np.random.randint(1, 1000), np.random.randint(1, 1000)
c, d = div_loop(a, b)
if c == a // b and d == a % b:
pass
else:
print("False at: ", a, b)