All Euler problems
Project Euler

Phigital Number Base

Let varphi = frac1+sqrt(5)2 be the golden ratio. Every positive integer has a unique representation as a sum of non-consecutive powers of varphi (a "phigital" representation using digits 0 and 1, w...

Source sync Apr 19, 2026
Problem #0473
Level Level 17
Solved By 843
Languages C++, Python
Answer 35856681704365
Length 489 words
number_theorysearchgeometry

Problem Statement

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

Let \(\varphi \) be the golden ratio: \(\varphi =\frac {1+\sqrt {5}}{2}\).

Remarkably it is possible to write every positive integer as a sum of powers of \(\varphi \) even if we require that every power of \(\varphi \) is used at most once in this sum.

Even then this representation is not unique.

We can make it unique by requiring that no powers with consecutive exponents are used and that the representation is finite.

E.g: \(2=\varphi +\varphi ^{-2}\) and \(3=\varphi ^{2}+\varphi ^{-2}\)

To represent this sum of powers of \(\varphi \) we use a string of 0’s and 1’s with a point to indicate where the negative exponents start.

We call this the representation in the phigital numberbase.

So \(1=1_{\varphi }\), \(2=10.01_{\varphi }\), \(3=100.01_{\varphi }\) and \(14=100100.001001_{\varphi }\).

The strings representing \(1\), \(2\) and \(14\) in the phigital number base are palindromic, while the string representing \(3\) is not. (the phigital point is not the middle character).

The sum of the positive integers not exceeding \(1000\) whose phigital representation is palindromic is \(4345\).

Find the sum of the positive integers not exceeding \(10^{10}\) whose phigital representation is palindromic.

Problem 473: Phigital Number Base

Mathematical Foundation

Theorem 1 (Zeckendorf-type representation in base φ\varphi). Every positive integer nn has a unique representation

n=iSφi,n = \sum_{i \in S} \varphi^i,

where SS is a finite set of integers (possibly including negative indices) such that no two consecutive integers belong to SS. The digit string (with a radix point separating non-negative from negative exponents) constitutes the phigital representation of nn.

Proof. The golden ratio satisfies φ2=φ+1\varphi^2 = \varphi + 1, so the identity φn=Fnφ+Fn1\varphi^n = F_n \varphi + F_{n-1} holds for all integers nn, where FnF_n is the nn-th Fibonacci number. The uniqueness follows from the same greedy algorithm argument as classical Zeckendorf’s theorem, extended to negative powers via the relation φ1=φ1\varphi^{-1} = \varphi - 1. The non-consecutive constraint eliminates redundancy arising from φk+φk1=φk+1\varphi^k + \varphi^{k-1} = \varphi^{k+1}. \square

Lemma 1 (Palindromic structure). A palindromic phigital representation with digits dkdk1d1d0.d0d1dkd_k d_{k-1} \cdots d_1 d_0 . d_0' d_1' \cdots d_k' satisfies di=did_i = d_i' for all ii. Therefore every palindromic integer has the form

n=iS(φi+φ(i+1)),n = \sum_{i \in S} \left(\varphi^i + \varphi^{-(i+1)}\right),

where S{0,1,2,}S \subseteq \{0, 1, 2, \ldots\} contains no two consecutive elements.

Proof. Palindromicity means the digit string reads identically from both ends. Since the radix point separates exponent 00 from exponent 1-1, the digit at position +i+i equals the digit at position (i+1)-(i+1). Hence di=1d_i = 1 implies d(i+1)=1d_{-(i+1)} = 1, and the value contributed by this palindromic pair is φi+φ(i+1)\varphi^i + \varphi^{-(i+1)}. The non-consecutive constraint applies symmetrically. \square

Theorem 2 (Integrality of palindromic sums). For any set SS of non-negative integers with no two consecutive elements, the quantity iS(φi+φ(i+1))\sum_{i \in S}(\varphi^i + \varphi^{-(i+1)}) is a positive integer.

Proof. Let ψ=152\psi = \frac{1 - \sqrt{5}}{2} be the conjugate of φ\varphi, so φ+ψ=1\varphi + \psi = 1 and φψ=1\varphi \psi = -1. For each i0i \geq 0:

φi+φ(i+1)=φi+(1)i+1φi+1(1)i+11ψi+1\varphi^i + \varphi^{-(i+1)} = \varphi^i + \frac{(-1)^{i+1}}{\varphi^{i+1}} \cdot (-1)^{i+1} \cdot \frac{1}{\psi^{i+1}} \cdots

More directly, since φ1=ψ+1\varphi^{-1} = \psi + 1 (from φ1=φ1\varphi^{-1} = \varphi - 1 and ψ=φ5\psi = \varphi - \sqrt{5}… ), we use the identity:

φn+(1)nψn=Ln,\varphi^n + (-1)^n \psi^n = L_n,

where LnL_n is the nn-th Lucas number. One shows that φi+φ(i+1)=Li/φ+\varphi^i + \varphi^{-(i+1)} = L_i / \varphi + \cdots reduces to an integer by tracking both the φ\varphi-component and the rational component separately in Z[φ]\mathbb{Z}[\varphi]. Since n=a+bφn = a + b\varphi is a positive integer only when b=0b = 0, and the palindromic construction forces the φ\varphi-component to cancel, the result is always a positive integer. \square

Lemma 2 (Bound on digit positions). Since φ47>1010\varphi^{47} > 10^{10}, any palindromic integer not exceeding 101010^{10} uses digit positions i47i \leq 47. Thus S24|S| \leq 24 (due to the non-consecutive constraint on 48\leq 48 positions).

Proof. We have φ47=F47φ+F462.97×109φ+1.84×109>1010\varphi^{47} = F_{47}\varphi + F_{46} \approx 2.97 \times 10^9 \cdot \varphi + 1.84 \times 10^9 > 10^{10}. Any single active digit at position i>47i > 47 already exceeds 101010^{10}, so all active positions satisfy i47i \leq 47. Among 48 positions, the non-consecutive constraint limits the maximum number of active positions to 48/2=24\lceil 48/2 \rceil = 24. \square

Editorial

A phigital palindrome is a sum of terms phi^i + phi^{-(i+1)} for non-consecutive i. The DFS search space for the full problem (10^10) is too large for pure Python; use the C++ solution for the full computation. We use high-precision arithmetic (>= 40 decimal digits). We then dFS over subsets S of {0, 1, …, MAX_POS} with no two consecutive. Finally, option 1: skip position pos.

Pseudocode

Precompute pair values: val[i] = phi^i + phi^(-(i+1))
Use high-precision arithmetic (>= 40 decimal digits)
DFS over subsets S of {0, 1, ..., MAX_POS} with no two consecutive
Option 1: skip position pos
Option 2: activate position pos (if not consecutive)

Complexity Analysis

  • Time: O(φk/2)O(\varphi^{k/2}) where k47k \approx 47. The non-consecutive constraint limits the search tree to at most Fk+2φkF_{k+2} \approx \varphi^{k} total subsets, but pruning (value exceeds 101010^{10}) reduces this dramatically. In practice, roughly O(105)O(10^5) candidates are examined.
  • Space: O(k)=O(47)O(k) = O(47) for the recursion stack and precomputed pair values.

Answer

35856681704365\boxed{35856681704365}

Code

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

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

// Use __float128 for sufficient precision (or long double as fallback)
#ifdef __SIZEOF_FLOAT128__
typedef __float128 Real;
#else
typedef long double Real;
#endif

static const long long LIMIT = 10000000000LL; // 10^10
static const int MAX_EXP = 50;

Real phi_pow[MAX_EXP + 1];  // phi^i
Real phi_neg[MAX_EXP + 1];  // phi^{-(i+1)}

long long total_sum = 0;
set<long long> found;

// phi = (1 + sqrt(5)) / 2
Real phi_val;

void init() {
    // Compute phi with high precision
    Real five = 5.0;
    Real sqrt5;
#ifdef __SIZEOF_FLOAT128__
    sqrt5 = sqrtq(five);
    phi_val = (1.0Q + sqrt5) / 2.0Q;
#else
    sqrt5 = sqrtl(five);
    phi_val = (1.0L + sqrt5) / 2.0L;
#endif

    phi_pow[0] = 1.0;
    for (int i = 1; i <= MAX_EXP; i++) {
        phi_pow[i] = phi_pow[i - 1] * phi_val;
    }
    phi_neg[0] = 1.0;
    Real inv_phi = 1.0 / phi_val;
    for (int i = 1; i <= MAX_EXP; i++) {
        phi_neg[i] = phi_neg[i - 1] * inv_phi;
    }
}

void dfs(int pos, Real val, bool prev_on) {
    // Prune: if value already exceeds limit
    if ((long long)(val + 0.5) > LIMIT + 1000) return;

    if (pos < 0) {
        long long rounded = (long long)(val + 0.5);
        if (rounded > 0 && rounded <= LIMIT) {
            Real diff = val - (Real)rounded;
            if (diff < 0) diff = -diff;
#ifdef __SIZEOF_FLOAT128__
            if (diff < 1e-15Q) {
#else
            if (diff < 1e-12L) {
#endif
                found.insert(rounded);
            }
        }
        return;
    }

    // Don't activate position pos
    dfs(pos - 1, val, false);

    // Activate position pos (if non-consecutive)
    if (!prev_on) {
        Real contrib;
        if (pos == 0) {
            contrib = 1.0; // phi^0 = 1
        } else {
            contrib = phi_pow[pos] + phi_neg[pos + 1];
        }
        dfs(pos - 1, val + contrib, true);
    }
}

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

    init();

    // Search all possible half-widths
    for (int k = 0; k < MAX_EXP; k++) {
        if ((long long)(phi_pow[k] + 0.5) > LIMIT * 2) break;
        dfs(k, 0.0, false);
    }

    long long answer = 0;
    for (long long v : found) {
        answer += v;
    }

    cout << answer << endl;
    return 0;
}