39 lines
944 B
Python
39 lines
944 B
Python
from math import isqrt
|
|
|
|
|
|
def iq(n):
|
|
isq = isqrt(n)
|
|
return isq * isq == n
|
|
|
|
|
|
def euler_142():
|
|
# x + y x - y x + z x - z y + z y - z
|
|
NMAX = 1_000_000
|
|
squares = [n * n for n in range(1, NMAX)]
|
|
for x in range(1, NMAX):
|
|
for s1 in squares:
|
|
y = x - s1 # s1 = x - y
|
|
if y <= 0:
|
|
break
|
|
if not iq(x + y): # s2 = x + y
|
|
continue
|
|
for s3 in squares:
|
|
z = y - s3 # s3 = y - z
|
|
if x - z <= 0:
|
|
continue
|
|
if z <= 0:
|
|
break
|
|
if not iq(y + z): # s4 = y + z
|
|
continue
|
|
if iq(x + z) and iq(x - z):
|
|
print(x, y, z, x + y + z)
|
|
return x + y + z
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
solution = euler_142()
|
|
print("e142.py: " + str(solution))
|
|
assert solution == 1006193
|
|
|