0

A number is "prime" if it has no divisors other than itself and1. For example, 23 is prime
and 35 is not prime because 35 = 7 x 5. If the digits of a number are rearranged, then
usually its primeness changes - for example, 32 is not prime but 53 is. For this problem,
you have to find numbers which are prime no matter how you rearrange their digits. For
example, all of the numbers 113, 131 and 311are prime, so we say that 113 is an
"anagrammatic prime" (also 131 and 311 are anagrammatic primes).
Input will be from standard input and will consist of lines with a single positive number, n,
less than 10,000,000, on each line. You must find the first anagrammatic prime which is
bigger than n and less than the next power of 10 above n. The input will be terminated
by a line consisting of a single 0.
Output, which must be written to standard output, must be one number for each number
in the input. The number must either be the next anagrammatic prime, as described
above or 0 if there is no anagrammatic prime in the range defined.

Example

Input
10
16
900
113
0

Output
11
17
919
131
0

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by masijade
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.