-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path75.py
59 lines (48 loc) · 1.38 KB
/
75.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
limit = 1500000
solution = 0
wirelengths = list()
for i in xrange(0, limit+1):
wirelengths.append([])
coprime_pairs = list()
def farey(n, asc=True):
"""Print the nth Farey sequence, either ascending or descending."""
if asc:
a, b, c, d = 0, 1, 1, n # (*)
else:
a, b, c, d = 1, 1, n-1, n # (*)
print "%d/%d" % (a, b)
while (asc and c <= n) or (not asc and a > 0):
k = int((n + b)/d)
a, b, c, d = c, d, k*c - a, k*d - b
# print a, b
if a < b:
coprime_pairs.append((a, b))
print "%d/%d" % (a, b)
# m**2 + n**2 + m**2 - n **2 + 2*m*n <= limit
# 2*m**2 + 2*m*n <= limit
# m**2 + m*n <= limit/2
# m + n <= limit/(2*m)
# given this, the square root of the limit is sufficiently large
farey(int(limit**0.5))
for coprime_pair in coprime_pairs:
m = coprime_pair[1]
n = coprime_pair[0]
k = 1
while True:
a = k*(m**2 + n ** 2)
b = k*(2*m*n)
c = k*(m**2 - n ** 2)
wirelength = a + b + c
if wirelength <= limit:
triple = [a, b, c]
triple.sort()
if triple not in wirelengths[wirelength]:
wirelengths[wirelength].append(triple)
k += 1
else:
break
for wirelength in wirelengths:
if len(wirelength) == 1:
solution += 1
print e, wirelength
print solution