Fact Digit Sum
Submissions: 2278   Accuracy:

27.67%

  Difficulty: Easy   Marks: 2

Let's define A(n) for positive integer n as a sum of factorials of its digits. For example, A(154)=1!+ 5! +4!= 145.

Given a number 'x' , you need to print the minimum number L such that A(L)=x. If no such L exists, then print -1

Input:
First line contains 'T' - the number of test cases
Then each line contains an integer 'x'.

Output:
Print the answer for each value 'x'.

Constraints:
1<=T<=542
1000<=x<=1000000000

Example:
Input:

1
40321
Output:
18

Explanations:
A(18)=1!+ 8! =40321  and 18 is the smallest element for which A(18) is 40321
Note that A(80) = A(81) is also 40321, But among them 18 is the smallest number.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Abhinav Jain
Author: iamabjain


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.