Saturday, February 11, 2017

Write a Java Program to all Keith Number between 1 to 1000.

Note:A Keith Number is an integer N with ‘d’ digits with the following property:
If a Fibonacci-like sequence (in which each term in the sequence is the sum of the ‘d’ previous terms)
is formed, with the first ‘d’ terms being the decimal digits of the number N, then N itself occurs as a
term in the sequence.

For example, 197 is a Keith number since it generates the sequence
1, 9, 7, 17, 33, 57, 107, 197, ………..

Some keith numbers are: 14 ,19, 28 , 47 , 61, 75, 197, 742, 1104, 1537……………   */
import java.io.Console;
import static java.lang.System.out;
class Keith2
{
public static void main(String...alt)
{
int arr[]=new int[50];
for(int i=1;i<=1000;i++)
{
String s=Integer.toString(i);
int n=i,j=s.length();
while(n!=0)
{
int rem=n%10;
arr[j-1]=rem;
n/=10;
j--;
}
j=s.length();
int sum=0;
while(sum<i)
{
sum=0;
for(int k=1;k<=s.length();k++)
sum+=arr[j-k];
arr[j]=sum;
j++;
}
if(sum==i)
out.println(i);
}
}

}

Share this

0 Comment to "Write a Java Program to all Keith Number between 1 to 1000."

Post a Comment