Solve problem 142 and make 141 brute force work again.
This commit is contained in:
38
python/e142.py
Normal file
38
python/e142.py
Normal file
@@ -0,0 +1,38 @@
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user