1
1
"""
2
+ Problem 39: 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,19 +25,31 @@ 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
29
- if hypotenuse == int (( hypotenuse ) ):
32
+ if hypotenuse == int (hypotenuse ):
30
33
perimeter = int (base + perpendicular + hypotenuse )
31
34
if perimeter > max_perimeter :
32
35
continue
33
- else :
34
- triplets [perimeter ] += 1
36
+ triplets [perimeter ] += 1
35
37
return triplets
36
38
37
39
40
+ def solution (n : int = 1000 ) -> int :
41
+ """
42
+ Returns perimeter with maximum solutions.
43
+ >>> solution(100)
44
+ 90
45
+ >>> solution(200)
46
+ 180
47
+ >>> solution(1000)
48
+ 840
49
+ """
50
+ triplets = pythagorean_triple (n )
51
+ return triplets .most_common (1 )[0 ][0 ]
52
+
53
+
38
54
if __name__ == "__main__" :
39
- triplets = pythagorean_triple (1000 )
40
- print (f"{ triplets .most_common ()[0 ][0 ] = } " )
55
+ print (f"Perimeter { solution ()} has maximum solutions" )
0 commit comments