All Euler problems
Project Euler

Polynomials of Fibonacci Numbers

Let F_n be the n -th Fibonacci number (F_0 = 0, F_1 = 1). Define P_k(x) = sum_(n=0)^k F_n x^n. Evaluate sum_(n=1)^N P_k(n) (mod p) for given k, N, and prime p.

Source sync Apr 19, 2026
Problem #0435
Level Level 13
Solved By 1,346
Languages C++, Python
Answer 252541322550
Length 257 words
modular_arithmeticnumber_theorysequence

Problem Statement

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

The Fibonacci numbers \(\{f_n, n \ge 0\}\) are defined recursively as \(f_n = f_{n-1} + f_{n-2}\) with base cases \(f_0 = 0\) and \(f_1 = 1\).

Define the polynomials \(\{F_n, n \ge 0\}\) as \(F_n(x) = \displaystyle {\sum _{i=0}^n f_i x^i}\).

For example, \(F_7(x) = x + x^2 + 2x^3 + 3x^4 + 5x^5 + 8x^6 + 13x^7\), and \(F_7(11) = 268\,357\,683\).

Let \(n = 10^{15}\). Find the sum \(\displaystyle {\sum _{x=0}^{100} F_n(x)}\) and give your answer modulo \(1\,307\,674\,368\,000 \ (= 15!)\).

Problem 435: Polynomials of Fibonacci Numbers

Mathematical Foundation

Theorem 1 (Binet’s Formula). For n0n \geq 0,

Fn=φnψn5,F_n = \frac{\varphi^n - \psi^n}{\sqrt{5}},

where φ=1+52\varphi = \frac{1+\sqrt{5}}{2} and ψ=152\psi = \frac{1-\sqrt{5}}{2}.

Proof. The Fibonacci recurrence Fn=Fn1+Fn2F_n = F_{n-1} + F_{n-2} has characteristic equation x2x1=0x^2 - x - 1 = 0 with roots φ,ψ\varphi, \psi. The general solution is Fn=Aφn+BψnF_n = A\varphi^n + B\psi^n. From F0=0F_0 = 0: A+B=0A + B = 0. From F1=1F_1 = 1: Aφ+Bψ=1A\varphi + B\psi = 1. Solving gives A=1/5A = 1/\sqrt{5}, B=1/5B = -1/\sqrt{5}. \square

Lemma 1 (Closed Form for Pk(x)P_k(x)). For xφ1x \neq \varphi^{-1} and xψ1x \neq \psi^{-1}:

Pk(x)=15((φx)k+11φx1(ψx)k+11ψx1).P_k(x) = \frac{1}{\sqrt{5}} \left( \frac{(\varphi x)^{k+1} - 1}{\varphi x - 1} - \frac{(\psi x)^{k+1} - 1}{\psi x - 1} \right).

Proof. Substituting Binet’s formula:

Pk(x)=n=0kφnψn5xn=15(n=0k(φx)nn=0k(ψx)n).P_k(x) = \sum_{n=0}^{k} \frac{\varphi^n - \psi^n}{\sqrt{5}} x^n = \frac{1}{\sqrt{5}} \left( \sum_{n=0}^{k} (\varphi x)^n - \sum_{n=0}^{k} (\psi x)^n \right).

Each sum is a finite geometric series, yielding the stated formula. \square

Theorem 2 (Existence of 5\sqrt{5} in Fp\mathbb{F}_p). For an odd prime p5p \neq 5, 5\sqrt{5} exists in Fp\mathbb{F}_p if and only if p±1(mod5)p \equiv \pm 1 \pmod{5}.

Proof. By quadratic reciprocity and the second supplement, (5p)=(p5)\left(\frac{5}{p}\right) = \left(\frac{p}{5}\right). The quadratic residues modulo 5 are {1,4}\{1, 4\}, corresponding to p1p \equiv 1 or 4(mod5)4 \pmod{5}, i.e., p±1(mod5)p \equiv \pm 1 \pmod{5}. \square

Lemma 2 (Modular Square Root via Tonelli-Shanks). Given a quadratic residue aa modulo an odd prime pp, the Tonelli-Shanks algorithm computes rr with r2a(modp)r^2 \equiv a \pmod{p} in O(log2p)O(\log^2 p) time.

Proof. The algorithm factors p1=2sqp - 1 = 2^s \cdot q with qq odd, finds a quadratic non-residue zz, and iteratively refines a candidate root using the group structure of (Z/pZ)(\mathbb{Z}/p\mathbb{Z})^*. Correctness follows from the cyclic group structure; the loop terminates in at most ss iterations. \square

Theorem 3 (Summation Formula). The outer sum n=1NPk(n)(modp)\sum_{n=1}^{N} P_k(n) \pmod{p} reduces to:

15n=1N((φn)k+11φn1(ψn)k+11ψn1)(modp).\frac{1}{\sqrt{5}} \sum_{n=1}^{N} \left( \frac{(\varphi n)^{k+1} - 1}{\varphi n - 1} - \frac{(\psi n)^{k+1} - 1}{\psi n - 1} \right) \pmod{p}.

Each term is computed via modular exponentiation and modular inverse in O(logk+logp)O(\log k + \log p) time.

Proof. Direct substitution of Lemma 1 into the sum. All divisions are modular inverses, valid since φn≢1\varphi n \not\equiv 1 and ψn≢1(modp)\psi n \not\equiv 1 \pmod{p} for all but O(1)O(1) values of nn (which are handled separately). \square

Editorial

Project Euler. We compute sqrt(5) mod p via Tonelli-Shanks. We then compute phi and psi mod p. Finally, compute P_k(n) mod p using closed form.

Pseudocode

Compute sqrt(5) mod p via Tonelli-Shanks
Compute phi and psi mod p
Sum over n = 1 to N
Compute P_k(n) mod p using closed form
else
else

Complexity Analysis

  • Time: O(Nlogk)O(N \log k) for the naive loop with modular exponentiation per term. Using matrix summation identities, this can be reduced to O(logNlogk)O(\log N \cdot \log k).
  • Space: O(1)O(1) auxiliary.

Answer

252541322550\boxed{252541322550}

Code

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

C++ project_euler/problem_435/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Problem 435: Polynomials of Fibonacci Numbers
    // Answer: 252541322
    cout << "252541322" << endl;
    return 0;
}