Project Euler Problem9
Special Pythagorean triplet
Problem 9
A Pythagorean triplet is a set of three natural numbers, a
b
c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
The code is simple:
a = 0
b = 0
c = 0
totalSum = 1000
cMax = int(totalSum/2)
cMin = int(totalSum/3)
aMax = cMin
c = cMax
while c > cMin:
b = c - 1
while b > 1:
a = totalSum - c - b
if a > b or a < 1 or a > aMax:
break
if a*a + b*b == c*c:
print(a, b, c)
print(a*b*c)
break
b -= 1
c -= 1
浙公网安备 33010602011771号