-
Notifications
You must be signed in to change notification settings - Fork 0
/
p86.py
67 lines (55 loc) · 1.33 KB
/
p86.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
import euler_tools
import math
def compute_square_distance(a, b, c):
return (a + b)**2 + c**2
def is_integer_solution(a, b, c):
csd = compute_square_distance
min_square_distance = min(csd(a,b,c), csd(b,c,a), csd(c,a,b))
return euler_tools.is_sqr(min_square_distance)
def compute_integer_solutions(m):
solution_count = 0
i = 1
while i <= m:
j = i
while j <= m:
k = j
while k <= m:
if is_integer_solution(i, j, k):
solution_count += 1
k += 1
j += 1
i += 1
return solution_count
def compute_mid(imin, imax):
if imax == -1:
return imin * 2
return (imin + imax) / 2
#Takes too long
def solve(target = 10**6):
imin = 1000
imax = -1
closest_over = -1
while imax == -1 or imax >= imin:
imid = compute_mid(imin, imax)
count = compute_integer_solutions(imid)
if count < target:
imin = imid + 1
if count > target:
imax = imid - 1
closest_over = imid
return closest_over
def solve_2(target = 10**6):
length = 2
count = 0
while count < target:
length += 1
wh = 3
while wh <= 2 * length:
square_distance = wh**2 + length**2
if euler_tools.is_sqr(square_distance):
if wh <= length:
count += wh / 2
else:
count += 1 + (length - (wh + 1) / 2)
wh += 1
return length