Vampire Numbers
Time limit10sMemory limit128 MB
Given X, find the smallest vampire number at or above X, where a vampire number splits into two factors whose combined digits match its own digits.
- Level
Medium4 of 10
- Topics
- Brute force, Implementation, Math
- Solved
- No attempts yet
Problem
is an interesting number because , and the digits used on the left-hand side and the right-hand side are exactly the same. has a similar property: .
Numbers like these are called vampire numbers. That is, for to be a vampire number it must be possible to write it as a product of two numbers and () such that the digits appearing in and together are exactly the digits of , counting repetitions. None of , , may have a leading zero.
Because and would normally have to be the same length, would have to have an even number of digits; but in this problem, and having different lengths is also allowed for a vampire number.
Here are some examples of vampire numbers.
Given a number , write a program that finds the smallest vampire number greater than or equal to .
Input
The input consists of several test cases. Each test case is a single line containing an integer (). The input ends with a line containing .
Output
For each test case, output the smallest vampire number greater than or equal to , one per line.
Hint
Vampire numbers are a genuine mathematical concept (see Wikipedia: Vampire number).