Skip to content

Commit 8ddd7c2

Browse files
authored
improvements for project euler task 39
1 parent 6541236 commit 8ddd7c2

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Diff for: project_euler/problem_39/sol1.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
https://projecteuler.net/problem=39
3+
24
If p is the perimeter of a right angle triangle with integral length sides,
35
{a,b,c}, there are exactly three solutions for p = 120.
46
{20,48,52}, {24,45,51}, {30,40,50}
@@ -8,10 +10,11 @@
810

911
from __future__ import annotations
1012

13+
import typing
1114
from collections import Counter
1215

1316

14-
def pythagorean_triple(max_perimeter: int) -> dict:
17+
def pythagorean_triple(max_perimeter: int) -> typing.Counter[int]:
1518
"""
1619
Returns a dictionary with keys as the perimeter of a right angled triangle
1720
and value as the number of corresponding triplets.
@@ -22,7 +25,7 @@ def pythagorean_triple(max_perimeter: int) -> dict:
2225
>>> pythagorean_triple(50)
2326
Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1})
2427
"""
25-
triplets = Counter()
28+
triplets: typing.Counter[int] = Counter()
2629
for base in range(1, max_perimeter + 1):
2730
for perpendicular in range(base, max_perimeter + 1):
2831
hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5
@@ -35,6 +38,13 @@ def pythagorean_triple(max_perimeter: int) -> dict:
3538
return triplets
3639

3740

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+
3848
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

Comments
 (0)