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.
Input : x = 197 Output : Yes 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 : No 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 : Yes 14 is a Keith number since it appears in the sequence, 1, 4, 5, 9, 14, ...
- Store the ‘n’ digits of given number “x” in an array “terms”.
- Loop for generating next terms of sequence and adding the previous ‘n’ terms.
- Keep storing the next_terms from step 2 in array “terms”.
- If the next term becomes equal to x, then x is a Keith number. If next term becomes more than x, then x is not a Keith Number.
Yes No Yes
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Maximum number formed from array with K number of adjacent swaps allowed
- Find minimum number to be divided to make a number a perfect square
- Given number of matches played, find number of teams in tournament
- Number of times the largest perfect square number can be subtracted from N
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Build Lowest Number by Removing n digits from a given number
- Find the smallest number whose digits multiply to a given number n
- Find the largest good number in the divisors of given number N
- Print a number strictly less than a given number such that all its digits are distinct.
- Number of digits to be removed to make a number divisible by 3
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find count of digits in a number that divide the number
- Check if a number is divisible by all prime divisors of another number
Improved By : Mithun Kumar