Skills101
← All coding problems

Strong Numbers in a Range

easyPlacement Coding+10 XPTime limit 2s

A number is strong if the sum of the factorials of its digits equals the number. 145 is strong: 1! + 4! + 5! = 1 + 24 + 120 = 145.

Print every strong number from a to b.

Input: one line with a and b.

Output: the strong numbers in increasing order separated by spaces, or -1 if there are none.

Constraints: 1 ≤ a ≤ b ≤ 10⁹.

Think about it: checking every number up to 10⁹ is far too slow. A number with d digits has a digit-factorial sum of at most d · 9!. Find the point beyond which that sum can never catch up with the number itself — then you only need to search below it (or precompute the answers).

Sample input 1

1 200

Sample output 1

1 2 145

Sample input 2

146 40000

Sample output 2

-1

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.