-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-009.py
More file actions
37 lines (28 loc) · 839 Bytes
/
problem-009.py
File metadata and controls
37 lines (28 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
Problem 9 - Special Pythagorean Triplet
A Pythagorean triplet is a set of three natural numbers, a < b < c, for
which, a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
"""
def pythagorean_triplet(n: int) -> int:
"""
Parameters
n (int): the sum of a + b + c
Returns
prod (int): product a*b*c
"""
prod = 0
for a in range(1, n + 1):
for b in range(a, n + 1):
c = n - a - b
if (a ** 2 + b ** 2 == c ** 2) and (a + b + c == 1000):
prod = a * b * c
break
return prod
if __name__ == "__main__":
print(
"The Pythagorean triplet for which a + b + c = 1000 is: "
+ str(pythagorean_triplet(1000))
)