All Euler problems
Project Euler

Double-base Palindromes

Find the sum of all natural numbers less than 10^6 that are palindromic in both base 10 and base 2. Leading zeros are not permitted in either representation.

Source sync Apr 19, 2026
Problem #0036
Level Level 01
Solved By 97,584
Languages C++, Python
Answer 872187
Length 494 words
constructivebrute_forcemodular_arithmetic

Problem Statement

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

The decimal number, \(585 = 1001001001_2\) (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base \(10\) and base \(2\).

(Please note that the palindromic number, in either base, may not include leading zeros.)

Problem 36: Double-base Palindromes

Mathematical Development

Definitions

Definition 1. Let b2b \ge 2 be an integer. A positive integer nn is a palindrome in base bb if its base-bb representation (ak,ak1,,a1,a0)b(a_k, a_{k-1}, \ldots, a_1, a_0)_b with ak0a_k \ne 0 satisfies ai=akia_i = a_{k-i} for all 0ik0 \le i \le k.

Definition 2. A positive integer nn is a double-base palindrome if it is simultaneously a palindrome in base 10 and a palindrome in base 2.

Theoretical Development

Theorem 1 (Parity constraint). Every double-base palindrome is odd.

Proof. Let nn be a binary palindrome with representation (bk,bk1,,b0)2(b_k, b_{k-1}, \ldots, b_0)_2, where bk=1b_k = 1 (since leading zeros are excluded). By Definition 1, b0=bk=1b_0 = b_k = 1. Since n=i=0kbi2in = \sum_{i=0}^{k} b_i \cdot 2^i and b0=1b_0 = 1, we have n1(mod2)n \equiv 1 \pmod{2}. \blacksquare

Corollary 1.1. All even numbers can be excluded from the search, halving the candidate space.

Lemma 1 (Enumeration of base-10 palindromes below N=106N = 10^6). The number of base-10 palindromes in {1,2,,N1}\{1, 2, \ldots, N-1\} is exactly 19981998, partitioned by digit count as follows:

Digits ddCanonical formFree digitsCount
1(a)(a)a[1,9]a \in [1,9]9
2(aa)(a\,a)a[1,9]a \in [1,9]9
3(aba)(a\,b\,a)a[1,9],  b[0,9]a \in [1,9],\; b \in [0,9]90
4(abba)(a\,b\,b\,a)a[1,9],  b[0,9]a \in [1,9],\; b \in [0,9]90
5(abcba)(a\,b\,c\,b\,a)a[1,9],  b,c[0,9]a \in [1,9],\; b,c \in [0,9]900
6(abccba)(a\,b\,c\,c\,b\,a)a[1,9],  b,c[0,9]a \in [1,9],\; b,c \in [0,9]900

Proof. A dd-digit palindrome is uniquely determined by its first d/2\lceil d/2 \rceil digits. The leading digit a1a_1 ranges over {1,,9}\{1,\ldots,9\} (9 choices), and each subsequent free digit ranges over {0,,9}\{0,\ldots,9\} (10 choices each). For dd digits, the count is 910d/219 \cdot 10^{\lceil d/2 \rceil - 1}. Summing: 9+9+90+90+900+900=19989 + 9 + 90 + 90 + 900 + 900 = 1998. \blacksquare

Lemma 2 (Binary palindrome test). A positive integer nn is a binary palindrome if and only if rev2(n)=n\mathrm{rev}_2(n) = n, where rev2(n)\mathrm{rev}_2(n) denotes the integer obtained by reversing the binary digits of nn. This test requires Θ(log2n+1)\Theta(\lfloor \log_2 n \rfloor + 1) bit operations.

Proof. Let nn have =log2n+1\ell = \lfloor \log_2 n \rfloor + 1 binary digits. Construct rev2(n)\mathrm{rev}_2(n) by iterating: extract the least significant bit of nn via nmod2n \bmod 2, append to an accumulator via left-shift-and-add, then right-shift nn. After \ell iterations, the accumulator equals rev2(n)\mathrm{rev}_2(n). By Definition 1, nn is a binary palindrome iff this reversed value equals nn. Each iteration performs O(1)O(1) work, giving Θ()\Theta(\ell) total. \blacksquare

Theorem 2 (Efficiency of palindrome generation). Generating all P=1998P = 1998 base-10 palindromes below N=106N = 10^6 and testing each for the binary palindrome property requires O(PlogN)O(P \log N) operations, which is strictly less than the O(NlogN)O(N \log N) cost of testing every integer below NN.

Proof. Each binary palindrome test on a number n<Nn < N costs O(log2N)20O(\log_2 N) \le 20. Testing all generated palindromes: 199820=39,9601998 \cdot 20 = 39{,}960 operations. The brute-force alternative tests 10610^6 numbers at cost 10620=2×10710^6 \cdot 20 = 2 \times 10^7. Since P=1998106=NP = 1998 \ll 10^6 = N, the generation approach is superior by a factor of N/P500N/P \approx 500. \blacksquare

Editorial

We avoid scanning all integers below 10610^6 by generating only the decimal palindromes in that range. Each such palindrome is obtained by mirroring its free leading digits, and then its binary representation is tested for the same palindromic property. Summing exactly those candidates that pass both tests yields the required total.

Pseudocode

Algorithm: Sum of Double-base Palindromes
Require: The bound 10^6.
Ensure: The sum of all numbers below 10^6 that are palindromic in both base 10 and base 2.
1: Initialize total ← 0.
2: Generate every decimal palindrome below 10^6 by mirroring a positive seed, considering both even and odd lengths.
3: For each generated palindrome p, compute its binary expansion.
4: If that binary expansion is also palindromic, update total ← total + p.
5: Return total.

Complexity Analysis

Proposition. The algorithm runs in O(PlogN)O(P \cdot \log N) time and O(1)O(1) auxiliary space, where P=1998P = 1998 and N=106N = 10^6.

Proof. The generation phase enumerates exactly PP palindromes using nested loops with O(1)O(1) work per palindrome (arithmetic construction, no string operations needed). Each palindrome is tested via REV2\mathrm{REV}_2 in O(log2N)=O(20)O(\log_2 N) = O(20) time (Lemma 2). No arrays or sieves are required; only a running sum and loop variables are maintained. Total time: O(PlogN)O(P \cdot \log N). Auxiliary space: O(1)O(1). \blacksquare

Answer

872187\boxed{872187}

Code

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

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

bool isBinaryPalindrome(int n) {
    int rev = 0, orig = n;
    while (n > 0) {
        rev = (rev << 1) | (n & 1);
        n >>= 1;
    }
    return rev == orig;
}

int main() {
    int total = 0;

    // 1-digit palindromes
    for (int a = 1; a <= 9; a++)
        if (isBinaryPalindrome(a)) total += a;

    // 2-digit: aa
    for (int a = 1; a <= 9; a++) {
        int n = a * 11;
        if (isBinaryPalindrome(n)) total += n;
    }

    // 3-digit: aba
    for (int a = 1; a <= 9; a++)
        for (int b = 0; b <= 9; b++) {
            int n = a * 101 + b * 10;
            if (isBinaryPalindrome(n)) total += n;
        }

    // 4-digit: abba
    for (int a = 1; a <= 9; a++)
        for (int b = 0; b <= 9; b++) {
            int n = a * 1001 + b * 110;
            if (isBinaryPalindrome(n)) total += n;
        }

    // 5-digit: abcba
    for (int a = 1; a <= 9; a++)
        for (int b = 0; b <= 9; b++)
            for (int c = 0; c <= 9; c++) {
                int n = a * 10001 + b * 1010 + c * 100;
                if (isBinaryPalindrome(n)) total += n;
            }

    // 6-digit: abccba
    for (int a = 1; a <= 9; a++)
        for (int b = 0; b <= 9; b++)
            for (int c = 0; c <= 9; c++) {
                int n = a * 100001 + b * 10010 + c * 1100;
                if (isBinaryPalindrome(n)) total += n;
            }

    cout << total << endl;
    return 0;
}