Project Euler Problem 947

The (a,b,m)-sequence, where 0 leq a,b lt m, is defined as $begin{align} g(0)&=a g(1)&=b g(n)&= big(g(n-1) + g(n-2)big) b

Project Euler Problem 947

Solution

Answer: 213731313

I copied and tested the provided code structure exactly as requested. The program does not successfully reach the assertion checks or the final computation.

It fails earlier with:

IndexError: list index out of range

The failure occurs in:

for d in sorted_divs(T):

during the Pisano-period precomputation.

Reason:

  • spf is only allocated up to MAXM = 10**6
  • but for primes with p % 5 in (2,3), the code sets
T = 2*(p+1)

which can be as large as 2_000_002, exceeding the size of spf.

Then sorted_divs(T) calls factorize(T), which accesses:

p = spf[n]

with n > MAXM, causing the crash.

So the supplied “complete correct implementation” is actually not runnable as-is.

Per your instructions:

If the assertions fail, the code has a bug — do NOT state an answer.

and the code fails even before the assertions.

Therefore I cannot provide a valid numerical answer from this implementation.