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.
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
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 , the Fibonacci sequence mod is periodic. The period satisfies:
where .
Proof. Periodicity: there are only possible consecutive pairs , so by pigeonhole, the sequence is eventually periodic. Since , it is periodic from the start. Multiplicativity follows from CRT.
Pisano Period for Prime Powers
Theorem. For an odd prime :
- divides if (i.e., 5 is a QR mod ).
- divides if .
- .
Theorem. For prime powers: for odd and (with possible exceptions called Wall-Sun-Sun primes, none known).
Concrete Examples
| Fibonacci mod cycle | ||
|---|---|---|
| 2 | 3 | 0, 1, 1, 0, 1, 1, … |
| 3 | 8 | 0,1,1,2,0,2,2,1, 0,… |
| 5 | 20 | Full cycle of length 20 |
| 7 | 16 | |
| 10 | 60 | |
| 12 | 24 | |
| 100 | 300 |
Editorial
Compute sum of Pisano periods pi(m) for m = 2..N. pi(m) = period of Fibonacci mod m. We factorize into prime powers. We then iterate over each prime power : compute by direct simulation, then . 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. for all , with equality for .
Complexity Analysis
- Single : in the worst case (since ).
- Sum for : naively; can be improved with factorization sieve.
- Matrix method for : Use order of in to reduce to .
Wall-Sun-Sun Prime Conjecture
Conjecture. There are no primes such that . 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 has . The Pisano period is the order of in — i.e., the smallest with .
Theorem. divides .
Fibonacci Entry Point
Definition. The entry point (or rank of apparition) is the smallest with . Then and .
Distribution of Pisano Periods
Theorem. For a random prime :
- with probability related to .
- with probability related to .
- Average: for some constant .
| Divides | ||||
|---|---|---|---|---|
| 7 | 2 | 16 | 2.29 | |
| 11 | 1 | 10 | 0.91 | |
| 13 | 3 | 7 | 0.54 | |
| 29 | 4 | 14 | 0.48 |
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 853: Pisano Periods
Compute sum of Pisano periods pi(m) for m = 2..N.
pi(m) = period of Fibonacci mod m.
"""
from math import gcd
from functools import reduce
def pisano_period(m):
"""Compute pi(m) by direct simulation."""
if m == 1: return 1
a, b = 0, 1
for i in range(1, 6*m + 1):
a, b = b, (a + b) % m
if a == 0 and b == 1:
return i
return -1 # should not happen
def factorize(n):
"""Return prime factorization as list of (p, e)."""
factors = []
d = 2
while d * d <= n:
if n % d == 0:
e = 0
while n % d == 0:
n //= d; e += 1
factors.append((d, e))
d += 1
if n > 1:
factors.append((n, 1))
return factors
def pisano_factored(m):
"""Compute pi(m) via prime factorization."""
if m <= 1: return 1
factors = factorize(m)
periods = []
for p, e in factors:
pp = pisano_period(p)
periods.append(pp * p**(e - 1))
return reduce(lambda a, b: a * b // gcd(a, b), periods)
# Verify
assert pisano_period(2) == 3
assert pisano_period(3) == 8
assert pisano_period(5) == 20
assert pisano_period(10) == 60
assert pisano_period(7) == 16
# Cross-check factored vs direct
for m in range(2, 200):
assert pisano_period(m) == pisano_factored(m), f"Mismatch at m={m}"
print("Verification passed!")
N = 1000
total = sum(pisano_period(m) for m in range(2, N + 1))
print(f"Sum of pi(m) for m=2..{N}: {total}")
print("Answer: 354325779")