Keith Number
Last Updated :
11 Sep, 2023
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:
- 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.
C++
#include<bits/stdc++.h>
using namespace std;
bool isKeith( int x)
{
vector < int > terms;
int temp = x, n = 0;
while (temp > 0)
{
terms.push_back(temp%10);
temp = temp/10;
n++;
}
reverse(terms.begin(), terms.end());
int next_term = 0, i = n;
while (next_term < x)
{
next_term = 0;
for ( int j=1; j<=n; j++)
next_term += terms[i-j];
terms.push_back(next_term);
i++;
}
return (next_term == x);
}
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static boolean isKeith( int x)
{
ArrayList<Integer> terms= new ArrayList<Integer>();
int temp = x, n = 0 ;
while (temp > 0 )
{
terms.add(temp% 10 );
temp = temp/ 10 ;
n++;
}
Collections.reverse(terms);
int next_term = 0 , i = n;
while (next_term < x)
{
next_term = 0 ;
for ( int j= 1 ; j<=n; j++)
next_term += terms.get(i-j);
terms.add(next_term);
i++;
}
return (next_term == x);
}
public static void main(String[] args)
{
if (isKeith( 14 ))
System.out.println( "Yes" );
else
System.out.println( "No" );
if (isKeith( 12 ))
System.out.println( "Yes" );
else
System.out.println( "No" );
if (isKeith( 197 ))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isKeith(x):
terms = [];
temp = x;
n = 0 ;
while (temp > 0 ):
terms.append(temp % 10 );
temp = int (temp / 10 );
n + = 1 ;
terms.reverse();
next_term = 0 ;
i = n;
while (next_term < x):
next_term = 0 ;
for j in range ( 1 ,n + 1 ):
next_term + = terms[i - j];
terms.append(next_term);
i + = 1 ;
return (next_term = = x);
print ( "Yes" ) if (isKeith( 14 )) else print ( "No" );
print ( "Yes" ) if (isKeith( 12 )) else print ( "No" );
print ( "Yes" ) if (isKeith( 197 )) else print ( "No" );
|
C#
using System;
using System.Collections;
class GFG{
static bool isKeith( int x)
{
ArrayList terms = new ArrayList();
int temp = x, n = 0;
while (temp > 0)
{
terms.Add(temp%10);
temp = temp/10;
n++;
}
terms.Reverse();
int next_term = 0, i = n;
while (next_term < x)
{
next_term = 0;
for ( int j=1; j<=n; j++)
next_term += ( int )terms[i-j];
terms.Add(next_term);
i++;
}
return (next_term == x);
}
public static void Main()
{
if (isKeith(14))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
if (isKeith(12))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
if (isKeith(197))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isKeith( $x )
{
$terms = array ();
$temp = $x ;
$n = 0;
while ( $temp > 0)
{
array_push ( $terms , $temp % 10);
$temp = (int)( $temp / 10);
$n ++;
}
$terms = array_reverse ( $terms );
$next_term = 0;
$i = $n ;
while ( $next_term < $x )
{
$next_term = 0;
for ( $j = 1; $j <= $n ; $j ++)
$next_term += $terms [ $i - $j ];
array_push ( $terms , $next_term );
$i ++;
}
return ( $next_term == $x );
}
isKeith(14) ? print ( "Yes\n" ) : print ( "No\n" );
isKeith(12) ? print ( "Yes\n" ) : print ( "No\n" );
isKeith(197) ? print ( "Yes\n" ) : print ( "No\n" );
?>
|
Javascript
<script>
function isKeith(x)
{
let terms = [];
let temp = x;
let n = 0;
while (temp > 0)
{
terms.push(temp % 10);
temp = parseInt(temp / 10);
n++;
}
terms= terms.reverse();
let next_term = 0;
let i = n;
while (next_term < x)
{
next_term = 0;
for (let j = 1; j <= n; j++)
next_term += terms[i - j];
terms.push(next_term);
i++;
}
return (next_term == x);
}
isKeith(14) ? document.write( "Yes<br>" ) :
document.write( "No<br>" );
isKeith(12) ? document.write( "Yes<br>" ) :
document.write( "No<br>" );
isKeith(197) ? document.write( "Yes<br>" ) :
document.write( "No<br>" );
</script>
|
Output:
Yes
No
Yes
Time complexity: O(n^2) where n is no of digits
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...