def fib(): a, b = 1, 1 yield a yield b while True: a, b = b, a + b yield b def pisano_period(fibs, n): for period in range(2, 130): if fibs[0] % n == fibs[period] % n and fibs[1] % n == fibs[period + 1] % n: return period return None def euler_853(): r = 0 fs = fib() fibs = [next(fs) for _ in range(400)] upper = 10**9 pi_target = 120 for n in range(2, upper + 1): # Check if pi_target is a period. if fibs[0] % n == fibs[pi_target] % n and fibs[1] % n == fibs[pi_target + 1] % n: # Make sure that no lower period than pi_target exits. if pisano_period(fibs, n) == pi_target: r += n # print(n, prime_factors(n)) # I have noticed that the highest prime factor is always 61 or # 2521, so we could probably also get the solution by # enumerating the prime factors that yield a sum smaller than # upper and have 61 or 2521 as their last factor. return r if __name__ == "__main__": solution = euler_853() print("e853.py: " + str(solution)) assert solution == 44511058204