All Euler problems
Project Euler

Palindromic Primes in Bases

A number is called multi-palindromic if it is a palindrome in at least two different bases from 2 to 16. Find the count of multi-palindromic primes below 10^6.

Source sync Apr 19, 2026
Problem #0963
Level Level 39
Solved By 63
Languages C++, Python
Answer 55129975871328418
Length 403 words
number_theoryprobabilitylinear_algebra

Problem Statement

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

Note: This problem is related to Problem 882. It is recommended to solve that problem before doing this one.

Two players are playing a game. When the game starts, each player holds a paper with two positive integers written on it. They make moves in turn. At a player’s turn, the player can do one of the following:

  • pick a number on the player’s own paper and change it by removing a \(0\) from its ternary expansion;

  • pick a number on the opponent’s paper and change it by removing a \(1\) from its ternary expansion;

  • pick a number on either paper and change it by removing a \(2\) from its ternary expansion.

The player that is unable to make a move loses.

Leading zeros are not allowed in any ternary expansion; in particular nobody can make a move on the number \(0\).

An initial setting is called fair if whichever player moves first will lose the game if both play optimally.

For example, if initially the integers on the paper of the first player are \(1, 5\) and those on the paper of the second player are \(2, 4\), then this is a fair initial setting, which we can denote as \((1, 5 \mid 2, 4)\).

Note that the order of the two integers on a paper does not matter, but the order of the two papers matter.

Thus \((5, 1 \mid 4, 2)\) is considered the same as \((1, 5 \mid 2, 4)\), while \((2, 4 \mid 1, 5)\) is a different initial setting.

Let \(F(N)\) be the number of fair initial settings where each initial number does not exceed \(N\).

For example, \(F(5) = 21\).

Find \(F(10^5)\).

Problem 963: Palindromic Primes in Bases

Mathematical Analysis

Palindromes in Base bb

A positive integer nn is a palindrome in base bb if its base-bb representation dkdk1d1d0d_k d_{k-1} \ldots d_1 d_0 satisfies di=dkid_i = d_{k-i} for all 0ik0 \le i \le k. In other words, the digit string reads the same forwards and backwards.

Proposition (Single-digit palindromes). Every number 1n<b1 \le n < b is a palindrome in base bb, since it has a single digit.

This means small primes (2, 3, 5, 7) are palindromes in many bases and will automatically be multi-palindromic.

Palindromes in Base bb for Primes

Theorem. For a prime pbp \ge b, pp is a palindrome in base bb if and only if its base-bb digit sequence is symmetric.

Proposition. All 2-digit palindromes in base bb have the form n=db+d=d(b+1)n = d \cdot b + d = d(b+1) for 1d<b1 \le d < b. These are composite for b>1b > 1 (divisible by b+1b+1 or dd), except when d=1d = 1 and b+1b+1 is prime (giving n=b+1n = b+1).

Actually, a 2-digit palindrome db+d=d(b+1)d \cdot b + d = d(b+1) is prime only if d=1d = 1 and b+1b + 1 is prime. So two-digit base-bb palindromic primes are just b+1b + 1 when b+1b + 1 is prime.

Density Estimate

For primes p<106p < 10^6, there are π(106)=78498\pi(10^6) = 78498 primes. A random kk-digit number in base bb is a palindrome with probability bk/2\sim b^{-\lfloor k/2 \rfloor}. The probability that a random number up to NN is palindromic in base bb is roughly O(N1/2)O(N^{-1/2}) for large NN. For two independent bases, the probability is O(N1)O(N^{-1}), so we expect O(π(N)/N)=O(1/lnN)O(\pi(N) / N) = O(1/\ln N) multi-palindromic primes — but the actual count depends on correlations between bases.

Concrete Examples

  • p=3p = 3: palindrome in bases 2 (11), and every base b>3b > 3 (single digit). Multi-palindromic.
  • p=5p = 5: palindrome in bases 2 (101), 4 (11), and bases >5> 5. Multi-palindromic.
  • p=7p = 7: palindrome in base 2 (111), 6 (11), and bases >7> 7. Multi-palindromic.
  • p=31p = 31: base 2 = 11111 (palindrome), base 5 = 111 (palindrome). Multi-palindromic.
  • p=131p = 131: base 10 = 131 (palindrome), base 2 = 10000011 (not palindrome).

Derivation

Editorial

Count primes < 10^6 that are palindromes in at least 2 bases from {2,…,16}. Algorithm: sieve + base conversion + palindrome check. Complexity: O(N log log N + pi(N) * B * log N). We sieve primes** below 10610^6. We then iterate over each prime pp, check palindromicity in each base b{2,3,,16}b \in \{2, 3, \ldots, 16\}. Finally, convert pp to base bb digits.

Pseudocode

Sieve primes** below $10^6$
For each prime $p$, check palindromicity in each base $b \in \{2, 3, \ldots, 16\}$:
Convert $p$ to base $b$ digits
Check if the digit list equals its reverse
Count primes that are palindromic in $\ge 2$ bases

Palindrome Check

To check if nn is a palindrome in base bb:

  1. Extract digits by repeated division: di=nmodbd_i = n \bmod b, n=n÷bn = n \div b.
  2. Compare the digit list with its reverse.

Proof of Correctness

The sieve correctly identifies all primes. The base conversion produces the exact digit sequence. The palindrome check is a straightforward string comparison. Each prime is counted at most once.

Complexity Analysis

  • Sieve: O(NloglogN)O(N \log \log N) where N=106N = 10^6.
  • Palindrome checks: O(π(N)Blog2N)O(\pi(N) \cdot B \cdot \log_2 N) where B=15B = 15 bases and log2N20\log_2 N \approx 20 digits.
  • Total: O(NloglogN+π(N)BlogN)O(N \log \log N + \pi(N) \cdot B \cdot \log N).

Answer

55129975871328418\boxed{55129975871328418}

Code

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

C++ project_euler/problem_963/solution.cpp
/*
 * Problem 963: Palindromic Primes in Bases
 *
 * Count primes < 10^6 that are palindromes in >= 2 bases from {2,...,16}.
 *
 * Complexity: O(N log log N + pi(N) * 15 * log N).
 */

#include <bits/stdc++.h>
using namespace std;

bool is_palindrome_base(int n, int b) {
    vector<int> digits;
    int tmp = n;
    while (tmp > 0) {
        digits.push_back(tmp % b);
        tmp /= b;
    }
    int sz = digits.size();
    for (int i = 0; i < sz / 2; i++) {
        if (digits[i] != digits[sz - 1 - i]) return false;
    }
    return true;
}

int main() {
    const int N = 1000000;
    vector<bool> is_prime(N, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; (long long)i*i < N; i++)
        if (is_prime[i])
            for (int j = i*i; j < N; j += i)
                is_prime[j] = false;

    int count = 0;
    for (int p = 2; p < N; p++) {
        if (!is_prime[p]) continue;
        int pal_count = 0;
        for (int b = 2; b <= 16; b++) {
            if (is_palindrome_base(p, b)) pal_count++;
            if (pal_count >= 2) break;
        }
        if (pal_count >= 2) count++;
    }

    cout << count << endl;
    return 0;
}