-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem086.py
64 lines (58 loc) · 1.63 KB
/
problem086.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# The Euler Project: problem 086
#
from decorators import benchmark
from prime import gcd
from utils import is_square
import math
##
# Pitou: by unfolding the cube (a x b x c)
#
# ---------F
# | |
# | |
# F--------- ------ --------- ---c--F
# | | | | |
# | | | | |
# | | | b |
# | | | | |
# | | | | |
# --------- ------S----a---- ------
# | |
# | |
# ---------
# | |
# | |
# | |
# | |
# | |
# ---------F
#
# it is obvious that d(S,F)**2 is the min between
#
# a**2 + (b+c)**2
# and
# (a+c)**2 + b**2
#
# so the problem is reduce to a pythagore triplet problem
# where we need to find the pythagore triplet that verify x**2 + y**2 = z**2
# with x <= M and y <= 2*M
#
# so we take a <- [1..2*M]
# and if a**2 + M**2 is a square we store the possibilities that can lead to this
# then increase M 1 by 1
#N = 2000
N = 1e6
@benchmark
def solve():
c,m = 0,0
while c < N:
m += 1
for a in xrange(2,2*m+1):
if is_square(a*a + m*m):
c += (m + m + 2 - a)//2 if a > m+1 else a//2
return m,c
if __name__ == "__main__":
solve()