Friday 25 October 2013

Keith Number

/*To determine whether an n-digit number N is a Keith number, create a Fibonacci-like sequence that starts with the n decimal digits of N, putting the most significant digit first. Then continue the sequence, where each subsequent term is the sum of the previous n terms. By definition, N is a Keith number if N appears in the sequence thus constructed.
As an example, consider the 3-digit number N = 197. The sequence goes like this:
1, 9, 7, 17, 33, 57, 107, 197, 361, ...*/

import java.io.*;
class keith_number
{
public static void meth()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter number");
    int n=Integer.parseInt(br.readLine());
     int temp=n,t=n,r,re=-1,a,b,c; 
        while(n>0)
        {
            r=n%10;
            re++;
            n=n/10;
        }
        b=temp%((int)Math.pow(10,re));
        a=temp/((int)Math.pow(10,re));
          System.out.println(a);
          System.out.println(b) ;
             while(true)
             {
                 c=a+b;
                System.out.println(c) ;
      
                 if(c>=t)
                 break;
                   a=b;
                 b=c;
              
                }
                if (c==t)
                 System.out.println("keith number");
                 else
                 System.out.println("not a keith number");
              
                }
            }

1 comment: