-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBarrettReduction.py
55 lines (49 loc) · 1.04 KB
/
BarrettReduction.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
import sys
# a simple parser for python. use get_number() and get_word() to read
def parser():
while 1:
data = list(input().split(' '))
for number in data:
if len(number) > 0:
yield(number)
input_parser = parser()
def get_word():
global input_parser
return next(input_parser)
def get_number():
data = get_word()
try:
return int(data)
except ValueError:
return float(data)
a = get_number()
b = get_number()
found = False
low = 0
high = 2**31
while not found:
#def findIt(low, high):
mid = (low + high) >> 1
result = mid * a >> b
if result == 0:
low = mid
continue
elif result >= 2:
high = mid
continue
else: # result == 1
result2 = (mid - 1) * a >> b
if result2 == 0:
found = True
break
else:
high = mid
continue
upper = mid
#for i in range(upper + 1, 0, -1):
#result = i * a >> b
#if result == 0:
# print(i + 1)
#else
# a = 1/0
print(mid)