Solve some problems.
This commit is contained in:
32
python/e138.py
Normal file
32
python/e138.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from math import gcd
|
||||
|
||||
|
||||
def euler_138():
|
||||
count = 12
|
||||
r, m, n = 0, 2, 1
|
||||
while True:
|
||||
if count == 0:
|
||||
break
|
||||
|
||||
if (m - n) % 2 == 1 and gcd(m, n) == 1: # Ensure m and n are coprime and not both odd
|
||||
a = m * m - n * n
|
||||
b = 2 * m * n
|
||||
c = m * m + n * n
|
||||
if a > b:
|
||||
a, b = b, a
|
||||
assert a * a + b * b == c * c
|
||||
if a * 2 - 1 == b or a * 2 + 1 == b:
|
||||
# print(f"{m=} {n=} l={c}")
|
||||
r += c # c is L
|
||||
count -= 1
|
||||
n = m
|
||||
m *= 4
|
||||
m += 1
|
||||
return r
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
solution = euler_138()
|
||||
print("e138.py: " + str(solution))
|
||||
assert solution == 1118049290473932
|
||||
|
||||
Reference in New Issue
Block a user