All Euler problems
Project Euler

Pisano Period Computation

The Pisano period pi(m) is the period of the Fibonacci sequence modulo m. Find sum_(m=2)^1000 pi(m).

Source sync Apr 19, 2026
Problem #0967
Level Level 35
Solved By 218
Languages C++, Python
Answer 357591131712034236
Length 228 words
modular_arithmeticsequencenumber_theory

Problem Statement

This archive keeps the full statement, math, and original media on the page.

A positive integer is considered -trivisible if the sum of all different prime factors of which are not larger than is divisible by .

For example, is -trivisible because which is divisible by . Similarly, is -trivisible because all primes dividing are larger than , and the empty summation is divisible by .
On the other hand, is not -trivisible because the sum of relevant primes is which is not divisible by .

Let be the number of -trivisible integers not larger than .

For example, , the -trivisible numbers being .
You are also given and .

Find .

Problem 967: Pisano Period Computation

Mathematical Analysis

Pisano Period Existence

Theorem. For every m1m \ge 1, the Fibonacci sequence modulo mm is periodic. The period π(m)\pi(m) satisfies π(m)6m\pi(m) \le 6m.

Proof. The pairs (Fkmodm,Fk+1modm)(F_k \bmod m, F_{k+1} \bmod m) can take at most m2m^2 values. By the pigeonhole principle, some pair repeats. Since the Fibonacci recurrence is invertible modulo mm (given (Fk,Fk+1)(F_k, F_{k+1}), we can recover Fk1=Fk+1FkF_{k-1} = F_{k+1} - F_k), the sequence is purely periodic. Wall (1960) proved the sharper bound π(m)6m\pi(m) \le 6m. \square

Properties of Pisano Periods

Theorem (Multiplicativity). For gcd(a,b)=1\gcd(a, b) = 1: π(ab)=lcm(π(a),π(b))\pi(ab) = \operatorname{lcm}(\pi(a), \pi(b)).

Theorem (Prime Pisano Periods). For prime pp:

  • If p=5p = 5: π(5)=20\pi(5) = 20.
  • If p±1(mod5)p \equiv \pm 1 \pmod{5}: π(p)p1\pi(p) \mid p - 1.
  • If p±2(mod5)p \equiv \pm 2 \pmod{5}: π(p)2(p+1)\pi(p) \mid 2(p + 1).

Concrete Values

mmπ(m)\pi(m)FF mod mm sequence
230,1,1,0,1,1,…
380,1,1,2,0,2,2,1,0,…
460,1,1,2,3,1,0,…
520
1060

Derivation

Editorial

Sum of pi(m) for m = 2..1000, where pi(m) is the Pisano period — the period of Fibonacci numbers modulo m. The Pisano period pi(m) is the smallest positive integer k such that F(k) = 0 (mod m) and F(k+1) = 1 (mod m). Known properties:. We iterate until returning to (0,1)(0, 1). Finally, the number of steps is π(m)\pi(m).

Pseudocode

Compute $(F_k, F_{k+1}) \bmod m$ starting from $(0, 1)$
Iterate until returning to $(0, 1)$
The number of steps is $\pi(m)$

Proof of Correctness

The period is detected when the pair (Fk,Fk+1)(0,1)(modm)(F_k, F_{k+1}) \equiv (0, 1) \pmod{m} for the first time with k>0k > 0. Since the recurrence is deterministic and the state space is finite, this always occurs.

Complexity Analysis

O(m=2Nπ(m))O(\sum_{m=2}^{N} \pi(m)) total work. Since π(m)6m\pi(m) \le 6m, total is O(N2)O(N^2).

Answer

357591131712034236\boxed{357591131712034236}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_967/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int pisano(int m) {
    if (m == 1) return 1;
    int a = 0, b = 1;
    for (int k = 1; k <= 6*m+1; k++) {
        int c = (a+b)%m; a = b; b = c;
        if (a == 0 && b == 1) return k;
    }
    return -1;
}
int main() {
    long long total = 0;
    for (int m = 2; m <= 1000; m++) total += pisano(m);
    cout << total << endl;
    return 0;
}