from math import gcd def euler_139(): limit = 100_000_000 r, m, n = 0, 2, 1 while True: a = m * m - 1 * 1 b = 2 * m * 1 c = m * m + 1 * 1 if a + b + c > limit: return r for n in range(1, m): 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 ka, kb, kc = a, b, c while ka + kb + kc < limit: assert ka * ka + kb * kb == kc * kc if kc % (kb - ka) == 0: r += 1 ka, kb, kc = ka + a, kb + b, kc + c m += 1 if __name__ == "__main__": solution = euler_139() print("e139.py: " + str(solution)) assert solution == 10057761