-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path071_ordered_fractions.py
51 lines (35 loc) · 1.17 KB
/
071_ordered_fractions.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
"""
idea:
"""
import time
import math
def main():
t0 = time.time()
max_num = 3/7
closest = (0, 1)
closest_fraction = 0/1
for denominator in range(3, 1000000):
if denominator % 10000 == 0:
print('{}%, {}sec'.format(denominator/10000, round(time.time()-t0), 3))
start = int(math.floor(denominator * 3/7)) - 1
numerator = start
while (numerator+1) / denominator < max_num:
numerator +=1
if is_reduced(numerator, denominator) and numerator / denominator > closest_fraction:
closest_fraction = numerator / denominator
closest = numerator, denominator
#print(closest_fraction, closest)
print('real num: ', max_num)
print('best match:', closest_fraction)
print('final best matching:', closest)
def is_reduced(numerator, denominator):
return computeHCF(numerator, denominator) == 1
def computeHCF(x, y):
# This function implements the Euclidian algorithm to find H.C.F. of two numbers
while y:
x, y = y, x % y
return x
if __name__ == '__main__':
start_time = time.time()
main()
print("time:", time.time() - start_time)