-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathsol1.py
42 lines (36 loc) · 1.47 KB
/
sol1.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
"""
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?
"""
from typing import Dict
from collections import Counter
def pythagorean_triple(max_perimeter: int) -> Dict:
"""
Returns a dictionary with keys as the perimeter of a right angled triangle
and value as the number of corresponding triplets.
>>> pythagorean_triple(15)
Counter({12: 1})
>>> pythagorean_triple(40)
Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1})
>>> pythagorean_triple(50)
Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1})
"""
triplets = Counter()
for base in range(1, max_perimeter + 1):
for perpendicular in range(base, max_perimeter + 1):
hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5
if hypotenuse == int((hypotenuse)):
perimeter = int(base + perpendicular + hypotenuse)
if perimeter > max_perimeter:
continue
else:
if perimeter in triplets:
triplets[perimeter] += 1
else:
triplets[perimeter] = 1
return triplets
if __name__ == "__main__":
triplets = pythagorean_triple(1000)
print(max(triplets, key=lambda key: triplets[key]))