Special Pythagorean Triplet
# Project Euler - Question 9 - Special Pythagorean Triplet
# Written by Matthew Walker, 20 August 2017
# https://projecteuler.net/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.
# Answer = 31,875,000 Took 21 seconds. (10s with exit())
# Iterate a from 1 to 1000
# Then iterate b from a to 1000
# Then iterate c from b to 1000
# This is because a < b < c. Otherwise it would take
# much much longer and return two answers
for a in range(1,1000):
for b in range(a,1000):
for c in range (b, 1000):
# Once we have an iteration of a, b, and c
# Determine if it fits the criteria of a+b+c==1000 and
# a^2 + b^2 == c^2
# Test a+b+c first because it is a faster test. Takes ~half the time
if (a+b+c == 1000):
if (a*a + b*b == c*c):
# Print answers
print('A: ' + str(a) + ' B: ' + str(b) + ' C: ' + str(c))
print('Product is: ' + str(a*b*c))
# If we found it, exit to save time
exit()