All Euler problems
Project Euler

Modular Fibonacci Products

The Fibonacci sequence F_1 = 1, F_2 = 1, F_(n+2) = F_(n+1) + F_n. Compute: P(n, m) = prod_(k=1)^n F_k mod m for large n using the Pisano period (periodicity of Fibonacci mod m).

Source sync Apr 19, 2026
Problem #0885
Level Level 14
Solved By 1,177
Languages C++, Python
Answer 827850196
Length 406 words
modular_arithmeticsequencenumber_theory

Problem Statement

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

For a positive integer \(d\), let \(f(d)\) be the number created by sorting the digits of \(d\) in ascending order, removing any zeros. For example, \(f(3403) = 334\).

Let \(S(n)\) be the sum of \(f(d)\) for all positive integers \(d\) of \(n\) digits or less. You are given \(S(1) = 45\) and \(S(5) = 1543545675\).

Find \(S(18)\). Give you answer modulo \(11234556789\).

Problem 885: Modular Fibonacci Products

Mathematical Analysis

Definition (Pisano Period)

The Pisano period π(m)\pi(m) is the period of FnmodmF_n \bmod m. The sequence FnmodmF_n \bmod m is periodic with period π(m)\pi(m).

Theorem 1 (Existence and Properties)

  1. π(m)\pi(m) exists for all m2m \geq 2
  2. π(p)p21\pi(p) \mid p^2 - 1 for odd prime pp (more precisely, π(p)p1\pi(p) \mid p - 1 or π(p)2(p+1)\pi(p) \mid 2(p+1))
  3. π(2)=3\pi(2) = 3, π(5)=20\pi(5) = 20
  4. π(10)=60\pi(10) = 60 (last digit of Fibonacci repeats every 60)
  5. For m=p1a1pkakm = p_1^{a_1} \cdots p_k^{a_k}: π(m)=lcm(π(p1a1),,π(pkak))\pi(m) = \text{lcm}(\pi(p_1^{a_1}), \ldots, \pi(p_k^{a_k}))

Theorem 2 (Product Periodicity)

Let Qr=k=1rFkmodmQ_r = \prod_{k=1}^{r} F_k \bmod m for r=1,,π(m)r = 1, \ldots, \pi(m). If gcd(Qπ(m),m)=1\gcd(Q_{\pi(m)}, m) = 1, then the sequence P(n,m)modmP(n, m) \bmod m is eventually periodic.

Specifically, P(n,m)=Qπ(m)n/π(m)Qnmodπ(m)(modm)P(n, m) = Q_{\pi(m)}^{\lfloor n/\pi(m) \rfloor} \cdot Q_{n \bmod \pi(m)} \pmod{m}.

Theorem 3 (Fibonacci Product Identity)

k=1nFk=Fn+2!FF2!F\prod_{k=1}^{n} F_k = \frac{F_{n+2}! _F}{F_2! _F}

where n!F=k=1nFkn!_F = \prod_{k=1}^{n} F_k is the Fibonacci factorial (fibonorial).

Lemma (Small Pisano Periods)

mmπ(m)\pi(m)FkmodmF_k \bmod m cycle
231, 1, 0, 1, 1, 0, …
381, 1, 2, 0, 2, 2, 1, 0, …
5201, 1, 2, 3, 0, 3, 3, 1, 4, 0, …
716
1060

Concrete Numerical Examples

Fibonacci Products

nnFnF_nk=1nFk\prod_{k=1}^{n} F_kmod10\bmod 10
1111
2111
3222
4366
55300
682400
71331200
821655200

Note: Once F5=5F_5 = 5 appears, the product mod 10 stays 0.

Pisano Period Computation

mmπ(m)\pi(m)
23
38
46
520
624
716
812
924
1060
100300

Matrix Exponentiation for Single Fibonacci Values

The matrix identity (Fn+1FnFnFn1)=(1110)n\begin{pmatrix} F_{n+1} & F_n \\ F_n & F_{n-1} \end{pmatrix} = \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}^n allows computing FnmodmF_n \bmod m in O(logn)O(\log n) time via repeated squaring.

Proof of Matrix Identity

By induction: base case n=1n=1 is immediate. For n+1n+1:

(1110)n+1=(Fn+1FnFnFn1)(1110)=(Fn+1+FnFn+1Fn+Fn1Fn)=(Fn+2Fn+1Fn+1Fn)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}^{n+1} = \begin{pmatrix} F_{n+1} & F_n \\ F_n & F_{n-1} \end{pmatrix}\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix} = \begin{pmatrix} F_{n+1}+F_n & F_{n+1} \\ F_n+F_{n-1} & F_n \end{pmatrix} = \begin{pmatrix} F_{n+2} & F_{n+1} \\ F_{n+1} & F_n \end{pmatrix}

Important Fibonacci Product Identity

k=1nFk=F1F2Fn\prod_{k=1}^{n} F_k = F_1 \cdot F_2 \cdots F_n

The first few values: 1,1,2,6,30,240,3120,65520,1572480,1, 1, 2, 6, 30, 240, 3120, 65520, 1572480, \ldots

This sequence is sometimes called the fibonorial and appears in combinatorics (e.g., as a qq-analog of n!n! when q=ϕq = \phi).

Divisibility of Fibonacci Products

Since gcd(Fm,Fn)=Fgcd(m,n)\gcd(F_m, F_n) = F_{\gcd(m,n)} and pFp1p \mid F_{p-1} or pFp+1p \mid F_{p+1} for any odd prime pp, the product Fk\prod F_k acquires high powers of each prime factor relatively quickly.

Complexity Analysis

OperationTimeSpace
Compute π(m)\pi(m)O(π(m))O(\pi(m))O(1)O(1)
Compute P(n,m)P(n, m)O(π(m)+logn)O(\pi(m) + \log n)O(π(m))O(\pi(m))
Matrix method for FnmodmF_n \bmod mO(logn)O(\log n)O(1)O(1)

Answer

827850196\boxed{827850196}

Code

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

C++ project_euler/problem_885/solution.cpp
/*
 * Problem 885: Modular Fibonacci Products
 * Compute prod_{k=1}^{n} F_k mod m using Pisano period.
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int pisano(int m) {
    int prev = 0, curr = 1;
    for (int i = 1; i <= m * m; i++) {
        int next = (prev + curr) % m;
        prev = curr; curr = next;
        if (prev == 0 && curr == 1) return i;
    }
    return -1;
}

ll fib_product_mod(ll n, int m) {
    int pi = pisano(m);
    vector<ll> Q(pi + 1, 1);
    int a = 0, b = 1;
    for (int k = 1; k <= pi; k++) {
        int next = (a + b) % m;
        a = b; b = next;
        Q[k] = Q[k-1] * a % m;
    }
    ll full = n / pi;
    int rem = n % pi;
    ll result = 1;
    // Q[pi]^full mod m
    ll base = Q[pi]; ll exp = full;
    while (exp > 0) {
        if (exp & 1) result = result * base % m;
        base = base * base % m;
        exp >>= 1;
    }
    result = result * Q[rem] % m;
    return result;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cout << "=== Pisano Periods ===" << endl;
    for (int m = 2; m <= 20; m++)
        cout << "pi(" << m << ") = " << pisano(m) << endl;

    cout << "\n=== Products ===" << endl;
    for (int m : {7, 11, 13, 100, 1000}) {
        for (ll n : {10LL, 100LL, 1000LL}) {
            cout << "prod F_k mod " << m << " (n=" << n << ") = "
                 << fib_product_mod(n, m) << endl;
        }
    }

    int MOD = 1e9 + 7;
    cout << "\nAnswer: prod mod 10^9+7 (n=10^6) = " << fib_product_mod(1000000, MOD) << endl;
    return 0;
}