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
- Maximum number formed from array with K number of adjacent swaps allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Given number of matches played, find number of teams in tournament
- Number of ways to split a binary number such that every part is divisible by 2
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Count number of ways to divide a number in 4 parts
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Minimum number of given powers of 2 required to represent a number
- Querying maximum number of divisors that a number in a given range has
- Find maximum number that can be formed using digits of a given number
Improved By : Mithun Kumar