mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 09:10:16 +00:00
Fix coding style for Project Euler problem 39 (#3023)
* improvements for project euler task 39 * add tests for solution() * fixed a typo * Update sol1.py Co-authored-by: Dhruv <dhruvmanila@gmail.com>
This commit is contained in:
parent
c9500dc89f
commit
261be28120
|
@ -1,4 +1,6 @@
|
||||||
"""
|
"""
|
||||||
|
Problem 39: https://projecteuler.net/problem=39
|
||||||
|
|
||||||
If p is the perimeter of a right angle triangle with integral length sides,
|
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.
|
{a,b,c}, there are exactly three solutions for p = 120.
|
||||||
{20,48,52}, {24,45,51}, {30,40,50}
|
{20,48,52}, {24,45,51}, {30,40,50}
|
||||||
|
@ -8,10 +10,11 @@ For which value of p ≤ 1000, is the number of solutions maximised?
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import typing
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
|
|
||||||
|
|
||||||
def pythagorean_triple(max_perimeter: int) -> dict:
|
def pythagorean_triple(max_perimeter: int) -> typing.Counter[int]:
|
||||||
"""
|
"""
|
||||||
Returns a dictionary with keys as the perimeter of a right angled triangle
|
Returns a dictionary with keys as the perimeter of a right angled triangle
|
||||||
and value as the number of corresponding triplets.
|
and value as the number of corresponding triplets.
|
||||||
|
@ -22,19 +25,31 @@ def pythagorean_triple(max_perimeter: int) -> dict:
|
||||||
>>> pythagorean_triple(50)
|
>>> pythagorean_triple(50)
|
||||||
Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1})
|
Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1})
|
||||||
"""
|
"""
|
||||||
triplets = Counter()
|
triplets: typing.Counter[int] = Counter()
|
||||||
for base in range(1, max_perimeter + 1):
|
for base in range(1, max_perimeter + 1):
|
||||||
for perpendicular in range(base, max_perimeter + 1):
|
for perpendicular in range(base, max_perimeter + 1):
|
||||||
hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5
|
hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5
|
||||||
if hypotenuse == int((hypotenuse)):
|
if hypotenuse == int(hypotenuse):
|
||||||
perimeter = int(base + perpendicular + hypotenuse)
|
perimeter = int(base + perpendicular + hypotenuse)
|
||||||
if perimeter > max_perimeter:
|
if perimeter > max_perimeter:
|
||||||
continue
|
continue
|
||||||
else:
|
triplets[perimeter] += 1
|
||||||
triplets[perimeter] += 1
|
|
||||||
return triplets
|
return triplets
|
||||||
|
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
triplets = pythagorean_triple(1000)
|
print(f"Perimeter {solution()} has maximum solutions")
|
||||||
print(f"{triplets.most_common()[0][0] = }")
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user