All Euler problems
Project Euler

Pisano Periods

The Pisano period pi(m) is the period of the Fibonacci sequence modulo m: F_n mod m Compute sum_(m=2)^N pi(m) for a given bound N.

Source sync Apr 19, 2026
Problem #0853
Level Level 11
Solved By 1,718
Languages C++, Python
Answer 44511058204
Length 381 words
number_theorysequencemodular_arithmetic

Problem Statement

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

For every positive integer \(n\) the Fibonacci sequence modulo \(n\) is periodic. The period depends on the value of \(n\). This period is called the Pisano period for \(n\), often shortened to \(\pi (n)\).

There are three values of \(n\) for which \(\pi (n)\) equals \(18\): \(19\), \(38\) and \(76\). The sum of those smaller than \(50\) is \(57\).

Find the sum of the values of \(n\) smaller than \(1\,000\,000\,000\) for which \(\pi (n)\) equals \(120\).

Problem 853: Pisano Periods

Mathematical Analysis

Existence and Multiplicativity

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

π(m)=lcm(π(p1a1),,π(pkak))(1)\pi(m) = \text{lcm}(\pi(p_1^{a_1}), \ldots, \pi(p_k^{a_k})) \tag{1}

where m=p1a1pkakm = p_1^{a_1} \cdots p_k^{a_k}.

Proof. Periodicity: there are only m2m^2 possible consecutive pairs (Fn,Fn+1)modm(F_n, F_{n+1}) \bmod m, so by pigeonhole, the sequence is eventually periodic. Since Fn1=Fn+1FnF_{n-1} = F_{n+1} - F_n, it is periodic from the start. Multiplicativity follows from CRT. \square

Pisano Period for Prime Powers

Theorem. For an odd prime pp:

  • π(p)\pi(p) divides p1p - 1 if p±1(mod5)p \equiv \pm 1 \pmod{5} (i.e., 5 is a QR mod pp).
  • π(p)\pi(p) divides 2(p+1)2(p + 1) if p±2(mod5)p \equiv \pm 2 \pmod{5}.
  • π(5)=20\pi(5) = 20.

Theorem. For prime powers: π(pk)=pk1π(p)\pi(p^k) = p^{k-1} \pi(p) for pp odd and k1k \ge 1 (with possible exceptions called Wall-Sun-Sun primes, none known).

Concrete Examples

mmπ(m)\pi(m)Fibonacci mod mm cycle
230, 1, 1, 0, 1, 1, …
380,1,1,2,0,2,2,1, 0,…
520Full cycle of length 20
716
1060lcm(π(2),π(5))=lcm(3,20)=60\text{lcm}(\pi(2), \pi(5)) = \text{lcm}(3, 20) = 60
1224lcm(π(4),π(3))=lcm(6,8)=24\text{lcm}(\pi(4), \pi(3)) = \text{lcm}(6, 8) = 24
100300

Editorial

Compute sum of Pisano periods pi(m) for m = 2..N. pi(m) = period of Fibonacci mod m. We factorize mm into prime powers. We then iterate over each prime power pap^a: compute π(p)\pi(p) by direct simulation, then π(pa)=pa1π(p)\pi(p^a) = p^{a-1}\pi(p). Finally, combine via LCM.

Pseudocode

Factorize $m$ into prime powers
For each prime power $p^a$: compute $\pi(p)$ by direct simulation, then $\pi(p^a) = p^{a-1}\pi(p)$
Combine via LCM

Bound on Pisano Period

Theorem. π(m)6m\pi(m) \le 6m for all mm, with equality for m=25km = 2 \cdot 5^k.

Complexity Analysis

  • Single π(m)\pi(m): O(m)O(m) in the worst case (since π(m)6m\pi(m) \le 6m).
  • Sum π(m)\sum \pi(m) for mNm \le N: O(N2)O(N^2) naively; can be improved with factorization sieve.
  • Matrix method for π(p)\pi(p): Use order of (1110)\begin{pmatrix}1&1\\1&0\end{pmatrix} in GL2(Fp)GL_2(\mathbb{F}_p) to reduce to O(plogp)O(\sqrt{p} \log p).

Wall-Sun-Sun Prime Conjecture

Conjecture. There are no primes pp such that π(p2)pπ(p)\pi(p^2) \ne p \cdot \pi(p). Such primes (called Wall-Sun-Sun primes) would be related to Fermat’s Last Theorem for the first case.

Pisano Period and Fibonacci Matrix

The Fibonacci matrix M=(1110)M = \begin{pmatrix}1&1\\1&0\end{pmatrix} has Mn=(Fn+1FnFnFn1)M^n = \begin{pmatrix}F_{n+1}&F_n\\F_n&F_{n-1}\end{pmatrix}. The Pisano period π(m)\pi(m) is the order of MM in GL2(Z/mZ)GL_2(\mathbb{Z}/m\mathbb{Z}) — i.e., the smallest kk with MkI(modm)M^k \equiv I \pmod{m}.

Theorem. π(m)\pi(m) divides GL2(Z/mZ)=m2pm(11/p2)|\text{GL}_2(\mathbb{Z}/m\mathbb{Z})| = m^2 \prod_{p|m}(1 - 1/p^2).

Fibonacci Entry Point

Definition. The entry point (or rank of apparition) α(m)\alpha(m) is the smallest k>0k > 0 with mFkm \mid F_k. Then α(m)π(m)\alpha(m) \mid \pi(m) and π(m)/α(m){1,2,4}\pi(m) / \alpha(m) \in \{1, 2, 4\}.

Distribution of Pisano Periods

Theorem. For a random prime pp:

  • π(p)p1\pi(p) \mid p - 1 with probability related to p±1(mod5)p \equiv \pm 1 \pmod{5}.
  • π(p)2(p+1)\pi(p) \mid 2(p+1) with probability related to p±2(mod5)p \equiv \pm 2 \pmod{5}.
  • Average: π(p)Cp\pi(p) \approx C \cdot p for some constant CC.
pppmod5p \bmod 5Dividesπ(p)\pi(p)π(p)/p\pi(p)/p
722(p+1)=162(p+1)=16162.29
111p1=10p-1=10100.91
1332(p+1)=282(p+1)=2870.54
294p1=28p-1=28140.48

Answer

44511058204\boxed{44511058204}

Code

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

C++ project_euler/problem_853/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int pisano_period(int m) {
    if (m == 1) return 1;
    int a = 0, b = 1;
    for (int i = 1; i <= 6*m; i++) {
        int c = (a + b) % m;
        a = b; b = c;
        if (a == 0 && b == 1) return i;
    }
    return -1;
}

int main() {
    assert(pisano_period(2) == 3);
    assert(pisano_period(5) == 20);
    assert(pisano_period(10) == 60);

    int N = 1000;
    ll total = 0;
    for (int m = 2; m <= N; m++) total += pisano_period(m);
    cout << total << endl;
    return 0;
}