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.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
-
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.
Problem 963: Palindromic Primes in Bases
Mathematical Analysis
Palindromes in Base
A positive integer is a palindrome in base if its base- representation satisfies for all . In other words, the digit string reads the same forwards and backwards.
Proposition (Single-digit palindromes). Every number is a palindrome in base , 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 for Primes
Theorem. For a prime , is a palindrome in base if and only if its base- digit sequence is symmetric.
Proposition. All 2-digit palindromes in base have the form for . These are composite for (divisible by or ), except when and is prime (giving ).
Actually, a 2-digit palindrome is prime only if and is prime. So two-digit base- palindromic primes are just when is prime.
Density Estimate
For primes , there are primes. A random -digit number in base is a palindrome with probability . The probability that a random number up to is palindromic in base is roughly for large . For two independent bases, the probability is , so we expect multi-palindromic primes — but the actual count depends on correlations between bases.
Concrete Examples
- : palindrome in bases 2 (11), and every base (single digit). Multi-palindromic.
- : palindrome in bases 2 (101), 4 (11), and bases . Multi-palindromic.
- : palindrome in base 2 (111), 6 (11), and bases . Multi-palindromic.
- : base 2 = 11111 (palindrome), base 5 = 111 (palindrome). Multi-palindromic.
- : 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 . We then iterate over each prime , check palindromicity in each base . Finally, convert to base 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 is a palindrome in base :
- Extract digits by repeated division: , .
- 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: where .
- Palindrome checks: where bases and digits.
- Total: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
/*
* 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;
}
"""
Problem 963: Palindromic Primes in Bases
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).
"""
from math import isqrt
from collections import Counter
def sieve(n):
s = bytearray(b'\x01') * (n + 1)
s[0] = s[1] = 0
for i in range(2, isqrt(n) + 1):
if s[i]:
s[i*i::i] = bytearray(len(s[i*i::i]))
return [i for i in range(2, n + 1) if s[i]]
def to_base(n, b):
"""Convert n to list of digits in base b (least significant first)."""
if n == 0:
return [0]
digits = []
while n > 0:
digits.append(n % b)
n //= b
return digits
def is_palindrome_base(n, b):
"""Check if n is a palindrome in base b."""
digits = to_base(n, b)
return digits == digits[::-1]
def solve(N=10**6):
primes = sieve(N - 1)
count = 0
base_counts = Counter()
palindrome_counts = Counter()
for p in primes:
pal_bases = []
for b in range(2, 17):
if is_palindrome_base(p, b):
pal_bases.append(b)
base_counts[b] += 1
if len(pal_bases) >= 2:
count += 1
palindrome_counts[len(pal_bases)] += 1
return count, base_counts, palindrome_counts
# --- Compute answer ---
answer, base_counts, pal_counts = solve()
print(answer)