import time def sum_is_five(m, n): test = 3 *((3 * m - 1) * m + (3 * n - 1) * n) sqrt = int(test ** 0.5) + 1 if sqrt * (sqrt - 1) == test: q, r = divmod(sqrt * 2, 6) if not r: return q return 0 def gen_five(start): five = (3 * start - 1) * start // 2 while True: yield start, five five += 3 * start + 1 start += 1 def search(): stage = (0, 0, 0) for n, fn in gen_five(1): print(n, fn) for m, fm in gen_five(n + 1): if 3 * m + 1 > fn: break if 0 < stage[0] <= n: return stage x = sum_is_five(m, n) if x: if sum_is_five(m, x): return (n, m, x) if sum_is_five(n, x): stage = (m, n, x) print(search())