Keith Number

1.6

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 : 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, ...

Algorithm:

  1. Store the ‘n’ digits of given number “x” in an array “terms”.
  2. Loop for generating next terms of sequence and adding the previous ‘n’ terms.
  3. Keep storing the next_terms from step 2 in array “terms”.
  4. 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.

// C++ program to check if a number is Keith or not
// C++ program to check if a number is Keith or not
#include<bits/stdc++.h>
using namespace std;

// Returns true if x is Keith, else false.
bool isKeith(int x)
{
    // Store all digits of x in a vector "terms"
    // Also find number of digits and store in "n".
    vector <int> terms;
    int temp = x, n = 0; // n is number of digits in x
    while (temp > 0)
    {
        terms.push_back(temp%10);
        temp = temp/10;
        n++;
    }

    // To get digits in right order (from MSB to
    // LSB)
    reverse(terms.begin(), terms.end());

    // Keep finding next trms of a sequence generated
    // using digits of x until we either reach x or a
    // number greate than x
    int next_term = 0, i = n;
    while (next_term < x)
    {
        next_term = 0;

        // Next term is sum of previous n terms
        for (int j=1; j<=n; j++)
            next_term += terms[i-j];

        terms.push_back(next_term);
        i++;
    }

    /* When the control comes out of the while loop,
       either the next_term is equal to the number
       or greater than it.
       If next_term is equal to x, then x is a
       Keith number, else not */
    return (next_term == x);
}

// Driver program
int main()
{
   isKeith(14)? cout << "Yes\n" : cout << "No\n";
   isKeith(12)? cout << "Yes\n" : cout << "No\n";
   isKeith(197)? cout << "Yes\n" : cout << "No\n";
   return 0;
}

Output:

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 contribute@geeksforgeeks.org. 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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.6 Average Difficulty : 1.6/5.0
Based on 3 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.