Under construction

Still being built. Things may change

Palindromic Primes Category in Jeopardy!

Time limit1sMemory limit128 MB

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

The base bb 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?

Examples4

  1. Example 1

    Input
    1 10
    2 10
    3 10
    4 24
    5 4
    0 0
    
    Expected output
    The number of 1-digit palindromic primes < 2^31 in base 10.
    What is 4?
    
    The number of 2-digit palindromic primes < 2^31 in base 10.
    What is 1?
    
    The number of 3-digit palindromic primes < 2^31 in base 10.
    What is 15?
    
    The number of 4-digit palindromic primes < 2^31 in base 24.
    What is 0?
    
    The number of 5-digit palindromic primes < 2^31 in base 4.
    What is 10?
    
  2. Example 2

    Input
    1 10
    0 0
    
    Expected output
    The number of 1-digit palindromic primes < 2^31 in base 10.
    What is 4?
    
  3. Example 3

    Input
    1 2
    2 2
    3 2
    0 0
    
    Expected output
    The number of 1-digit palindromic primes < 2^31 in base 2.
    What is 0?
    
    The number of 2-digit palindromic primes < 2^31 in base 2.
    What is 1?
    
    The number of 3-digit palindromic primes < 2^31 in base 2.
    What is 2?
    
  4. Example 4

    Input
    1 3
    2 3
    3 3
    4 3
    5 3
    0 0
    
    Expected output
    The number of 1-digit palindromic primes < 2^31 in base 3.
    What is 1?
    
    The number of 2-digit palindromic primes < 2^31 in base 3.
    What is 0?
    
    The number of 3-digit palindromic primes < 2^31 in base 3.
    What is 2?
    
    The number of 4-digit palindromic primes < 2^31 in base 3.
    What is 0?
    
    The number of 5-digit palindromic primes < 2^31 in base 3.
    What is 3?