Palindromic Primes Category in Jeopardy!
Time limit1sMemory limit128 MB
Count n-digit numbers that are palindromic primes in a given base and below 2^31, reading digit/base pairs until 0 0.
- Level
Medium5 of 10
- Topics
- Number theory, Math, Brute force, Implementation
- Solved
- No attempts yet
Problem
A number is prime if it is greater than 1 and is evenly divisible only by 1 and itself. By definition, neither 0 nor 1 is prime.
A palindromic number is one whose string representation reads the same forwards and backwards.
You are on the clue crew preparing questions for the category "Palindromic Primes", writing a program that produces the Jeopardy!-style answer and its responding question.
For a given number of digits and a numeric base , count how many -digit numbers are, when written in base , both palindromes and prime and have a value less than . An -digit number has no leading zero, so its leading digit is nonzero.
Input
The input contains a series of number pairs separated by whitespace, ending with a pair of zeroes. For each pair, the first number is the number of digits to consider and the second number is the base in which the numbers are written.
Every palindromic prime counted in this problem fits in the range of a signed 32-bit integer (its value is less than ).
The base is an integer between 2 and 36. Bases above ten extend hexadecimal, so the valid digits are ['0'..'9'] and ['a'..'z'].
Output
For each pair, print two lines. The first line states the number of digits and the base (the Jeopardy!-style "answer"); the second line gives the count of palindromic primes found (the "question"). Separate consecutive pairs with a single blank line.
Use exactly this format, replacing n, b, and count with their values:
The number of n-digit palindromic primes < 2^31 in base b.
What is count?