Keith Number
Submissions: 519   Accuracy:

35.67%

  Difficulty: Easy   Marks: 2

A n digit number x is called Keith number if it appears in a special sequence (defined below) generated using its digits. The special sequence has first n terms as digits of x and other terms are recursively evaluated as sum of previous n terms.The task is to find if a given number is Keith Number or not.

Examples

Input : x = 197
Output : 1
197 has 3 digits, so n = 3
The number is Keith because it appears in the special
sequence that has first three terms as 1, 9, 7 and 
remaining terms evaluated using sum of previous 3 terms.
1, 9, 7, 17, 33, 57, 107, 197, .....

Input : x = 12
Output : 0
The number is not Keith because it doesn't appear in
the special sequence generated using its digits.
1, 2, 3, 5, 8, 13, 21, .....

Input : x = 14
Output : 1
14 is a Keith number since it appears in the sequence,
1, 4, 5, 9, 14, ...

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each line contains an integer x.

Output:
For each test case in a new line print 1 if the no is a Keith number else print 0.

Constraints:
1<=T<=10000
1<=x<=10000

Example:
Input:

2
14
10
Output:
1
0

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

Author: Shubham Joshi 1


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.