-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem66.py
79 lines (65 loc) · 1.9 KB
/
Problem66.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
'''
Diophantine Equation (25%)
6/12/19
158ms
'''
import time
import math
import re
start_time = time.perf_counter()
# Finding continued fraction sequence
max_n = 0
max_x = 0
for n in range(2, 1001):
if int(math.sqrt(n) + 0.5) ** 2 == n:
continue
# print('n ' + str(n))
seq = []
repeat = []
floor = 0
numerConst = 0
denom = 1
while True:
floor = int((math.sqrt(n) + numerConst) / denom)
numerConst = int(-(numerConst - floor * denom))
denom = int((n - numerConst ** 2) / denom)
string = str(floor) + ',' + str(numerConst) + ',' + str(denom)
if string in seq:
index = seq.index(string)
while len(seq) > index:
repeat.append(int(re.search('[0-9]+', seq.pop(index)).group()))
for i in range(len(seq)):
seq.append(
int(re.search('[0-9]+', seq.pop(len(seq) - 1)).group())
)
break
seq.append(string)
limit = 2
continued = seq.copy()
while len(continued) < limit:
elem = repeat.pop(0)
continued.append(elem)
repeat.append(elem)
while True:
# Calculating approximate fractions
numerator = 1
denominator = continued[limit - 1]
for i in range(limit - 2, -1, -1):
numerator += continued[i] * denominator
if i != 0:
temp = numerator
numerator = denominator
denominator = temp
# Check in Diophantine equation
if numerator ** 2 == n * (denominator ** 2) + 1:
if numerator > max_x:
max_x = numerator
max_n = n
break
elem = repeat.pop(0)
continued.append(elem)
repeat.append(elem)
limit += 1
print(max_n)
end_time = time.perf_counter()
print('ms: ' + '{:.0f}'.format((end_time - start_time) * 1000))