#!/usr/bin/env python3 def sols(): """yields int solutions of a**2 - 5 b**2 = 1""" a, b = 9, 4 while True: yield a, b a, b = 9*a + 20*b, 4*a + 9*b def cs(): """yields int solutions of a in a**2 - 5 b**2 = 44""" base_sols = [(7, 1), (8, 2), (13, 5), (17, 7), (32, 14), (43, 19)] base_sols.reverse() for a, b in sols(): for a0, b0 in base_sols: yield a0 * a - 5 * b0 * b N = 30 gen = cs() next(gen) nuggs = [] while len(nuggs) < N: c = next(gen) if (c - 7) % 5 != 0: continue nuggs.append((c-7) // 5) print(sum(nuggs))