-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path66.py
78 lines (62 loc) · 1.95 KB
/
66.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
## https://en.wikipedia.org/wiki/Pell's_equation
## https://projecteuler.net/problem=64
## https://projecteuler.net/problem=65
import math
def nplusfrac(a, rootn, minus, numer):
reciprocal = numer * (rootn**0.5 + minus) // (rootn - minus*minus)
nnumer = (rootn - minus*minus) // numer
nminus = (reciprocal * nnumer) - minus
return reciprocal, (rootn, nminus, nnumer)
def sqrt_period(n):
res = []
a0 = math.floor(n**0.5)
res.append(a0)
res.append([])
s = set()
a, rootn, minus, numer = a0, n, a0, 1
while (nplusfrac(a, rootn, minus, numer) not in s):
next_vals = nplusfrac(a, rootn, minus, numer)
s.add(next_vals)
res[1].append(next_vals[0])
a, rootn, minus, numer = next_vals[0], *next_vals[1]
return res
def improper_frac(a0, l):
def gcd(a, b):
if b == 0:
return a
return gcd(b, a%b)
def n_plus_frac(n, numer, denom):
nnumer = n * denom + numer
ndenom = denom
hcf = gcd(nnumer, ndenom)
nnumer //= hcf
ndenom //= hcf
return (ndenom, nnumer)
if len(l) == 1:
return n_plus_frac(a0, 1, l[0])
else:
imp_frac = n_plus_frac(l[-2], 1, l[-1])
for i in range(3, len(l)+1):
imp_frac = n_plus_frac(l[-i], *imp_frac)
imp_frac = n_plus_frac(a0, *imp_frac)
return imp_frac
def check_pell(D):
i = 0
period = sqrt_period(D)
period[0], period[1] = int(period[0]), list(map(int, period[1]))
l = len(period[1])
big_list = [period[1][i]]
y, x = improper_frac(period[0], big_list)
while (x*x - D*y*y != 1):
i += 1
big_list.append(period[1][i % l])
y, x = improper_frac(period[0], big_list)
return x
skip_set = set([x*x for x in range(1, 33)])
max_x = [0, 0]
for D in range(2, 1001):
if D not in skip_set:
x = check_pell(D)
if x > max_x[1]:
max_x = [D, x]
print(max_x)