-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_039.py
More file actions
39 lines (27 loc) · 923 Bytes
/
Copy pathproblem_039.py
File metadata and controls
39 lines (27 loc) · 923 Bytes
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
# coding: utf-8
'''
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}
For which value of p ≤ 1000, is the number of solutions maximised?
'''
# Analysis: a <= b < c => a < p/3
# a^2 + b^2 = c^2
# a + b + c = p
# ==> c = p – a – b
# a^2 + b^2 = (p - a - b)^2 = p^2 + a^2 + b^2 - 2pa - 2pb + 2ab ==> p^2 + 2pa + 2ab - 2pb = 0
# ==> b can be written: p(p - 2a) / 2(p - a)
def main():
perimeter = 0
max_solution = 0
for p in range(2, 1001):
nb_solution = 0
for a in range(2, p // 3):
if p * (p - 2 * a) % (2 * (p - a)) == 0:
nb_solution += 1
if nb_solution > max_solution:
max_solution = nb_solution
perimeter = p
return perimeter
if __name__ == '__main__':
print(main())
# 840 in 40.7ms