009 - Pythagorean triplets
Find the Pythagorean triplet (a62 + b^2 = c^2) for which a + b + c = 1000.
See Problem 9.
We have to search for this exhaustively, but the search can be optimised. c is at most 1000 - 2 (1000 - a - b) and at least 1000 / 3 (because it is greater than b and c). b can be no greater than c and together b & c cannot be more than 999. b is also at least half the remainder of 1000 - c (so that it is greater than a). a is 1000 less b and c:
pythag_sum = 1000
for c in xrange (pythag_sum-2, pythag_sum/3, -1):
for b in xrange (min (c-1, pythag_sum-c-1), (pythag_sum-c)/2, -1):
a = pythag_sum - c - b
if ((a**2 + b**2) == c**2):
print a, b, c
This gives the answer 200 375 425. Computation time is negligible.
