Correct problem 9 solution

This commit is contained in:
2019-09-29 18:03:11 +02:00
parent 3a09f3d4f5
commit 2cdbca922a
2 changed files with 64 additions and 15 deletions

View File

@ -10,6 +10,8 @@
#
# Find the product abc.
from math import gcd
from timeit import default_timer
def main():
@ -23,14 +25,37 @@ def main():
# Euclid's formula, until the one where a+b+c=1000 is found.
while not found:
for n in range(1, m):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if a + b + c == 1000:
found = 1
if found == 1:
break
if gcd(m, n) == 1 and (m % 2 == 0 and n % 2 != 0 or m % 2 != 0 and n % 2 == 0):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if a + b + c == 1000:
found = 1
break
i = 2
while True:
tmpa = a * i
tmpb = b * i
tmpc = c * i
if tmpa + tmpb + tmpc == 1000:
a = tmpa
b = tmpb
c = tmpc
found = 1
break
if tmpa + tmpb + tmpc > 1000:
break
i = i + 1
m = m + 1
end = default_timer()