-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem0009.py
53 lines (42 loc) · 1.32 KB
/
Problem0009.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Enonce = """
Special Pythagorean triplet
Problem 9
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 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
"""
import math
def main():
print(40*"=")
print(Enonce)
print(40*"-")
maxi = 1000
Pythagorean = []
#count = 0
c_min = math.ceil(maxi/3)
for c in range(c_min, maxi):
#count += 1
b_min = math.ceil((maxi - c)/2) if ((maxi - c)%2)!=0 else int((maxi - c)/2 + 1)
for b in range( b_min , min(c, maxi - c)):
#count += 1
a = maxi - b - c
#print(f"{a} < {b} < {c}")
if a >= b:
print(f"oups {a} < {b} < {c}")
continue
if (pow(a, 2) + pow(b, 2)) == pow(c, 2):
Pythagorean.append([a, b, c])
print(f"Pythagorean triplet found : {a} < {b} < {c}")
#print(count)
if len(Pythagorean) > 0:
a, b, c = Pythagorean[0]
Solution = a * b * c
print(f"The Pythagorean triplet for which a + b + c = 1000 is Pythagorean[0].")
print(40*"-")
print(f"Solution = {Solution}")
print(40*"=")
if __name__ == "__main__":
# execute only if run as a script
main()