Palindromic Primes Category in Jeopardy!

Time limit1sMemory limit128 MB

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 $n$ and a numeric base $b$, count how many $n$-digit numbers are, when written in base $b$, both palindromes and prime and have a value less than $2^{31}$. An $n$-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 $n$ to consider and the second number is the base $b$ 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 $2^{31}$).

The base $b$ 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?