-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler66.py
62 lines (42 loc) · 787 Bytes
/
Euler66.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
D = 1001
answer = 0
large = 0
for i in range(2, D):
sq = i ** 0.5
n = int(sq)
if n*n == i:
continue
set = {}
list = []
num = 1
den = n
t = (n, (num, den))
while t not in set:
set[t] = 1
list.append(t[0])
num = (i - (den*den))/num
n = int((sq + den) / num)
den = n * num - den
t = (n,(num, den))
list.pop(0)
prevX = 1
prevY = 0
x = int(sq)
y = 1
diophantine = x * x - i * y * y - 1
index = 0
while diophantine != 0:
tempX = x
tempY = y
x = (x * list[index]) + prevX
y = (y * list[index]) + prevY
index += 1
if index >= len(list):
index = 0
prevX = tempX
prevY = tempY
diophantine = x * x - i * y * y - 1
if x > large:
large = x
answer = i
print answer