MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / pythagorean_triple

Function pythagorean_triple

project_euler/problem_039/sol1.py:17–37  ·  view source on GitHub ↗

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

(max_perimeter: int)

Source from the content-addressed store, hash-verified

15
16
17def pythagorean_triple(max_perimeter: int) -> typing.Counter[int]:
18 """
19 Returns a dictionary with keys as the perimeter of a right angled triangle
20 and value as the number of corresponding triplets.
21 >>> pythagorean_triple(15)
22 Counter({12: 1})
23 >>> pythagorean_triple(40)
24 Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1})
25 >>> pythagorean_triple(50)
26 Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1})
27 """
28 triplets: typing.Counter[int] = Counter()
29 for base in range(1, max_perimeter + 1):
30 for perpendicular in range(base, max_perimeter + 1):
31 hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5
32 if hypotenuse == int(hypotenuse):
33 perimeter = int(base + perpendicular + hypotenuse)
34 if perimeter > max_perimeter:
35 continue
36 triplets[perimeter] += 1
37 return triplets
38
39
40def solution(n: int = 1000) -> int:

Callers 1

solutionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected