All Euler problems
Project Euler

Zeckendorf Representation Uniqueness

By Zeckendorf's theorem, every positive integer n has a unique representation as a sum of non-consecutive Fibonacci numbers. Let z(n) denote the number of Fibonacci summands in this representation....

Source sync Apr 19, 2026
Problem #0975
Level Level 38
Solved By 130
Languages C++, Python
Answer 88597366.47748
Length 392 words
sequencegreedylinear_algebra

Problem Statement

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

Given a pair of coprime odd positive integers, define the function It can be seen that , , and for all strictly between and .

Given two such pairs and , paths of infinitesimal width traverse the unit cube internally through every point such that . Remarkably, it can be shown that the point is always connected to the opposite corner . Furthermore, with the additional condition , it can be shown that there is exactly one path connecting the two points.

0975_examples.png

Shown above are two examples, as viewed from above the cube. That is, we see the paths projected onto the -plane, with corresponding values indicated with varying colour. In the second example some paths are coloured grey to indicate that, while they exist, they do not form part of the path from to .

Define to be the sum of the absolute changes in height (or -coordinate) over all uphill and downhill sections of the path from to . In the first example above, the path climbs over eleven uphill sections, and descends over ten downhill sections, giving . You are also given .

Let be the sum of over all pairs of primes, . You are given .

Find giving your answer rounded to five digits after the decimal point.

Problem 975: Zeckendorf Representation Uniqueness

Mathematical Foundation

Definition 1 (Fibonacci Numbers). Define F1=1,F2=2F_1 = 1, F_2 = 2, and Fk=Fk1+Fk2F_k = F_{k-1} + F_{k-2} for k3k \ge 3. (Equivalently, in the standard indexing: FkF_k here equals Fk+1F_{k+1} in the F1=F2=1F_1=F_2=1 convention.)

Theorem 1 (Zeckendorf, 1972). Every positive integer nn can be uniquely written as

n=Fk1+Fk2++Fkr,k1>k2+1,  k2>k3+1,  ,  kr1>kr+1,  kr1.n = F_{k_1} + F_{k_2} + \cdots + F_{k_r}, \quad k_1 > k_2 + 1, \; k_2 > k_3 + 1, \; \ldots, \; k_{r-1} > k_r + 1, \; k_r \ge 1.

Proof.

Existence (by strong induction on nn): For n=1=F1n = 1 = F_1, the representation is trivial. Suppose all integers less than nn have a valid representation. Let FmF_m be the largest Fibonacci number with FmnF_m \le n. If Fm=nF_m = n, we are done. Otherwise 0<nFm<Fm0 < n - F_m < F_m. Since Fmn<Fm+1=Fm+Fm1F_m \le n < F_{m+1} = F_m + F_{m-1}, we have nFm<Fm1n - F_m < F_{m-1}. By the inductive hypothesis, nFmn - F_m has a Zeckendorf representation using Fibonacci numbers at most Fm2F_{m-2} (since nFm<Fm1n - F_m < F_{m-1} implies the largest term is at most Fm2F_{m-2}). Prepending FmF_m gives a valid non-consecutive representation of nn.

Uniqueness (by strong induction on nn): Suppose n=Fk1++Fkr=F1++Fsn = F_{k_1} + \cdots + F_{k_r} = F_{\ell_1} + \cdots + F_{\ell_s} are two valid representations with k1>>krk_1 > \cdots > k_r and 1>>s\ell_1 > \cdots > \ell_s (all indices differing by at least 2). We claim k1=1k_1 = \ell_1. If k1>1k_1 > \ell_1, then:

nF1+F12+F14+n \le F_{\ell_1} + F_{\ell_1 - 2} + F_{\ell_1 - 4} + \cdots

But by the identity Fm+Fm2+Fm4+=Fm+11F_m + F_{m-2} + F_{m-4} + \cdots = F_{m+1} - 1 (telescoping from Fj=Fj+1Fj1F_j = F_{j+1} - F_{j-1}), the right side is at most F1+11<F1+1Fk1F_{\ell_1+1} - 1 < F_{\ell_1+1} \le F_{k_1}. This contradicts nFk1n \ge F_{k_1}. By symmetry, 1>k1\ell_1 > k_1 is also impossible, so k1=1k_1 = \ell_1. Canceling and applying the inductive hypothesis to nFk1n - F_{k_1} gives r=sr = s and ki=ik_i = \ell_i for all ii. \square

Lemma 1 (Greedy Algorithm Correctness). The greedy algorithm --- repeatedly subtracting the largest Fibonacci number not exceeding the remainder --- produces the Zeckendorf representation.

Proof. The existence proof above is constructive via the greedy algorithm. By uniqueness (Theorem 1), this must yield the Zeckendorf representation. \square

Theorem 2 (Average Number of Terms). Let ϕ=(1+5)/2\phi = (1+\sqrt{5})/2. The average value of z(n)z(n) for 1nFm11 \le n \le F_m - 1 satisfies

1Fm1n=1Fm1z(n)1ϕ+2logϕn0.27639logϕn\frac{1}{F_m - 1}\sum_{n=1}^{F_m - 1} z(n) \to \frac{1}{\phi + 2} \cdot \log_\phi n \approx 0.27639 \cdot \log_\phi n

as mm \to \infty.

Proof. (Sketch.) The Zeckendorf representation induces a bijection between {1,,Fm1}\{1, \ldots, F_m - 1\} and binary strings of length m1m - 1 with no consecutive 1s (the “Fibonacci numeral system”). The number of 1-bits in a random such string has mean m/(ϕ+2)\sim m/(\phi+2) by transfer matrix eigenvalue analysis. Since Fmϕm/5F_m \approx \phi^m/\sqrt{5}, we get mlogϕ(Fm5)m \approx \log_\phi(F_m\sqrt{5}), giving the stated asymptotic. \square

Editorial

Compute the sum of z(n) for n = 1 to 10^6, where z(n) is the number of terms in the Zeckendorf (greedy Fibonacci) representation of n. Every positive integer has a unique representation as a sum of non-consecutive Fibonacci numbers (Zeckendorf’s theorem). z(n) counts how many Fibonacci numbers are used. We precompute Fibonacci numbers up to N. We then fib = [1, 2, 3, 5, 8, 13, …] with fib[last] >= N. Finally, find largest fib[k] <= remainder.

Pseudocode

Precompute Fibonacci numbers up to N
fib = [1, 2, 3, 5, 8, 13, ...] with fib[last] >= N
Find largest fib[k] <= remainder

Complexity Analysis

  • Time: O(NlogϕN)O(N \log_\phi N). For each of the NN integers, the greedy decomposition uses at most O(logϕN)O(\log_\phi N) steps. For N=106N = 10^6, logϕ(106)29\log_\phi(10^6) \approx 29.
  • Space: O(logϕN)O(\log_\phi N) for the Fibonacci table (about 30 entries for N=106N = 10^6).

Answer

88597366.47748\boxed{88597366.47748}

Code

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

C++ project_euler/problem_975/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    const int N = 1000000;
    vector<long long> fibs = {1, 2};
    while (fibs.back() < N) fibs.push_back(fibs[fibs.size()-1]+fibs[fibs.size()-2]);
    long long total = 0;
    for (int n = 1; n <= N; n++) {
        int rem = n, cnt = 0;
        for (int i = fibs.size()-1; i >= 0; i--)
            if (fibs[i] <= rem) { rem -= fibs[i]; cnt++; }
        total += cnt;
    }
    cout << total << endl;
    return 0;
}