2020-08-18 10:49:02 +00:00
|
|
|
"""
|
2020-10-09 02:43:54 +00:00
|
|
|
Problem 39: https://projecteuler.net/problem=39
|
|
|
|
|
2020-08-18 10:49:02 +00:00
|
|
|
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?
|
|
|
|
"""
|
|
|
|
|
2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-10-09 02:43:54 +00:00
|
|
|
import typing
|
2020-08-18 10:49:02 +00:00
|
|
|
from collections import Counter
|
|
|
|
|
|
|
|
|
2020-10-09 02:43:54 +00:00
|
|
|
def pythagorean_triple(max_perimeter: int) -> typing.Counter[int]:
|
2020-08-18 10:49:02 +00:00
|
|
|
"""
|
|
|
|
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})
|
|
|
|
"""
|
2020-10-09 02:43:54 +00:00
|
|
|
triplets: typing.Counter[int] = Counter()
|
2020-08-18 10:49:02 +00:00
|
|
|
for base in range(1, max_perimeter + 1):
|
|
|
|
for perpendicular in range(base, max_perimeter + 1):
|
|
|
|
hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5
|
2020-10-09 02:43:54 +00:00
|
|
|
if hypotenuse == int(hypotenuse):
|
2020-08-18 10:49:02 +00:00
|
|
|
perimeter = int(base + perpendicular + hypotenuse)
|
|
|
|
if perimeter > max_perimeter:
|
|
|
|
continue
|
2020-10-09 02:43:54 +00:00
|
|
|
triplets[perimeter] += 1
|
2020-08-18 10:49:02 +00:00
|
|
|
return triplets
|
|
|
|
|
|
|
|
|
2020-10-09 02:43:54 +00:00
|
|
|
def solution(n: int = 1000) -> int:
|
|
|
|
"""
|
|
|
|
Returns perimeter with maximum solutions.
|
|
|
|
>>> solution(100)
|
|
|
|
90
|
|
|
|
>>> solution(200)
|
|
|
|
180
|
|
|
|
>>> solution(1000)
|
|
|
|
840
|
|
|
|
"""
|
|
|
|
triplets = pythagorean_triple(n)
|
|
|
|
return triplets.most_common(1)[0][0]
|
|
|
|
|
|
|
|
|
2020-08-18 10:49:02 +00:00
|
|
|
if __name__ == "__main__":
|
2020-10-09 02:43:54 +00:00
|
|
|
print(f"Perimeter {solution()} has maximum solutions")
|