1
1
"""
2
+ https://projecteuler.net/problem=39
3
+
2
4
If p is the perimeter of a right angle triangle with integral length sides,
3
5
{a,b,c}, there are exactly three solutions for p = 120.
4
6
{20,48,52}, {24,45,51}, {30,40,50}
8
10
9
11
from __future__ import annotations
10
12
13
+ import typing
11
14
from collections import Counter
12
15
13
16
14
- def pythagorean_triple (max_perimeter : int ) -> dict :
17
+ def pythagorean_triple (max_perimeter : int ) -> typing . Counter [ int ] :
15
18
"""
16
19
Returns a dictionary with keys as the perimeter of a right angled triangle
17
20
and value as the number of corresponding triplets.
@@ -22,7 +25,7 @@ def pythagorean_triple(max_perimeter: int) -> dict:
22
25
>>> pythagorean_triple(50)
23
26
Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1})
24
27
"""
25
- triplets = Counter ()
28
+ triplets : typing . Counter [ int ] = Counter ()
26
29
for base in range (1 , max_perimeter + 1 ):
27
30
for perpendicular in range (base , max_perimeter + 1 ):
28
31
hypotenuse = (base * base + perpendicular * perpendicular ) ** 0.5
@@ -35,6 +38,13 @@ def pythagorean_triple(max_perimeter: int) -> dict:
35
38
return triplets
36
39
37
40
41
+ def solution (n : int = 1000 ) -> int :
42
+ """Returns perimeter with maximum solutions."""
43
+ triplets = pythagorean_triple (n )
44
+ perimeter , _ = triplets .most_common ()[0 ]
45
+ return perimeter
46
+
47
+
38
48
if __name__ == "__main__" :
39
- triplets = pythagorean_triple (1000 )
40
- print (f"{ triplets . most_common ()[ 0 ][ 0 ] = } " )
49
+ result = solution (1000 )
50
+ print (f"Perimiter { result } has maximum solutions " )
0 commit comments