All Euler problems
Project Euler

Binary Palindrome Sums

A binary palindrome is a positive integer whose binary representation (with no leading zeros) is a palindrome. Find the sum of all binary palindromes less than 2^30.

Source sync Apr 19, 2026
Problem #0941
Level Level 38
Solved By 127
Languages C++, Python
Answer 1068765750
Length 295 words
linear_algebramodular_arithmeticbrute_force

Problem Statement

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

de Bruijn has a digital combination lock with \(k\) buttons numbered \(0\) to \(k-1\) where \(k \le 10\).

The lock opens when the last \(n\) buttons pressed match the preset combination.

Unfortunately he has forgotten the combination. He creates a sequence of these digits which contains every possible combination of length \(n\). Then by pressing the buttons in this order he is sure to open the lock.

Consider all sequences of shortest possible length that contains every possible combination of the digits.

Denote by \(C(k, n)\) the lexicographically smallest of these.

For example, \(C(3, 2) = \) 0010211220.

Define the sequence \(a_n\) by \(a_0=0\) and

\[a_n=(920461 a_{n-1}+800217387569)\bmod 10^{12} \text { for }\ n > 0\] Interpret each \(a_n\) as a \(12\)-digit combination, adding leading zeros for any \(a_n\) with less than \(12\) digits.

Given a positive integer \(N\), we are interested in the order the combinations \(a_1,\dots ,a_N\) appear in \(C(10,12)\).

Denote by \(p_n\) the place, numbered \(1,\dots ,N\), in which \(a_n\) appears out of \(a_1,\dots ,a_N\). Define \(\displaystyle F(N)=\sum _{n=1}^Np_na_n\).

For example, the combination \(a_1=800217387569\) is entered before \(a_2=696996536878\). Therefore: \[F(2)=1\cdot 800217387569 + 2\cdot 696996536878 = 2194210461325\] You are also given \(F(10)=32698850376317\).

Find \(F(10^7)\). Give your answer modulo \(1234567891\).

Problem 941: Binary Palindrome Sums

Mathematical Foundation

Definition 1. A positive integer nn is a binary palindrome if its binary representation bk1bk2b1b0b_{k-1} b_{k-2} \cdots b_1 b_0 satisfies bi=bk1ib_i = b_{k-1-i} for all 0ik10 \le i \le k-1, where bk1=1b_{k-1} = 1 (no leading zeros).

Theorem 1. The number of kk-bit binary palindromes is 2(k1)/22^{\lfloor (k-1)/2 \rfloor} for k1k \ge 1.

Proof. A kk-bit binary palindrome is determined by its first k/2\lceil k/2 \rceil bits. The leading bit bk1=1b_{k-1} = 1 is fixed. The remaining k/21=(k1)/2\lceil k/2 \rceil - 1 = \lfloor (k-1)/2 \rfloor bits are free, each independently 0 or 1. The second half is determined by symmetry: bi=bk1ib_i = b_{k-1-i}. Hence there are exactly 2(k1)/22^{\lfloor (k-1)/2 \rfloor} palindromes of bit-length kk. \square

Lemma 1. Every kk-bit binary palindrome with k30k \le 30 satisfies n<230n < 2^{30}, and every nn with 1n<2301 \le n < 2^{30} has bit-length at most 30.

Proof. A kk-bit number nn satisfies 2k1n<2k2^{k-1} \le n < 2^k. For k30k \le 30, we have n<230n < 2^{30}. Conversely, n<230n < 2^{30} implies nn has at most 30 bits. \square

Theorem 2 (Sum Formula). Let f=(k1)/2f = \lfloor (k-1)/2 \rfloor denote the number of free bits for kk-bit palindromes. The sum of all kk-bit binary palindromes is:

Sk=2f(2k1+1)+i=1f2f1(2k1i+2i)S_k = 2^f \cdot (2^{k-1} + 1) + \sum_{i=1}^{f} 2^{f-1} \cdot (2^{k-1-i} + 2^{i})

for even kk, and

Sk=2f2k1+2f12(k1)/2+i=1f2f1(2k1i+2i)S_k = 2^f \cdot 2^{k-1} + 2^{f-1} \cdot 2^{(k-1)/2} + \sum_{i=1}^{f} 2^{f-1} \cdot (2^{k-1-i} + 2^{i})

for odd k3k \ge 3 (with S1=1S_1 = 1).

Proof. Each palindrome has value V=2k1+(mirror contributions)+b0V = 2^{k-1} + (\text{mirror contributions}) + b_0 (for even kk, b0=bk1=1b_0 = b_{k-1} = 1). The leading and trailing bits contribute 2k1+12^{k-1} + 1 to each palindrome. Each free bit bib_i at position k1ik-1-i also appears at the mirror position ii, contributing 2k1i+2i2^{k-1-i} + 2^i when bi=1b_i = 1. Over all 2f2^f palindromes, each free bit is 1 in exactly half, giving factor 2f12^{f-1}. For odd kk, the middle bit at position (k1)/2(k-1)/2 is its own mirror. \square

Editorial

Compute the sum of all binary palindromes less than 2^30. A binary palindrome is a positive integer whose binary representation reads the same forwards and backwards (no leading zeros). Generation strategy: For each bit length k (1..30), generate palindromes by choosing the first ceil(k/2) bits (with MSB = 1), then mirroring to produce the full k-bit palindrome. Results:.

Pseudocode

    total = 0
    For k from 1 to max_bits:
        For half from 2^(ceil(k/2) - 1) to 2^ceil(k/2) - 1:
            palindrome = BuildPalindrome(half, k)
            total += palindrome
    Return total

    bits = binary_digits(half, ceil(k/2))
    full = bits + reverse(bits[0 : floor(k/2)]) // mirror first half
    Return binary_to_int(full)

Complexity Analysis

  • Time: O ⁣(k=1302(k1)/2)=O(215)O\!\left(\sum_{k=1}^{30} 2^{\lfloor (k-1)/2 \rfloor}\right) = O(2^{15}). With O(k)O(k) per palindrome for construction, total is O(30215)O(106)O(30 \cdot 2^{15}) \approx O(10^6).
  • Space: O(1)O(1) auxiliary (palindromes generated on the fly).

Answer

1068765750\boxed{1068765750}

Code

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

C++ project_euler/problem_941/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
    long long total=0;
    int cnt=0;
    for(int k=1;k<=30;k++){
        int half=(k+1)/2;
        for(int fh=(1<<(half-1));fh<(1<<half);fh++){
            long long val=0;
            vector<int> bits;
            int t=fh;
            for(int i=0;i<half;i++){bits.push_back(t&1);t>>=1;}
            reverse(bits.begin(),bits.end());
            vector<int> full=bits;
            if(k%2==1){
                for(int i=half-2;i>=0;i--) full.push_back(bits[i]);
            } else {
                for(int i=half-1;i>=0;i--) full.push_back(bits[i]);
            }
            if((int)full.size()!=k) continue;
            for(int b:full) val=val*2+b;
            if(val>0&&val<(1LL<<30)){total+=val;cnt++;}
        }
    }
    cout<<total<<endl;
    return 0;
}