SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Keith Number Program in Java and Python

12 January 2014

Keith Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number and check whether it is a Keith Number or not.

Note: A Keith Number is an integer N with d digits with the following property:

If a Fibonacci-like sequence, in which each term is the sum of the previous d 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 because 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

Algorithm:

Step 1: Start.

Step 2: Accept a number from the user.

Step 3: Store a copy of the number in another variable.

Step 4: Count the number of digits in the number and store it in d.

Step 5: Create an array to store the original digits and the generated Keith sequence terms.

Step 6: Extract the digits from right to left using the modulus operator and store them in the array from index d - 1 down to index 0.

Step 7: Set the next array position as d because the first d positions already contain the digits of the number.

Step 8: Add the previous d terms of the array to generate the next term of the sequence.

Step 9: Store the generated term in the array and move to the next position.

Step 10: Repeat Steps 8 and 9 while the generated term is less than the original number.

Step 11: If the generated term is equal to the original number, display that it is a Keith Number; otherwise, display that it is not a Keith Number.

Step 12: Stop.

Explanation:

The program first stores the input number in copy. This is necessary because the digits are extracted by repeatedly dividing the number by 10, and the original value n is required later for comparison.

The number of digits is stored in d. This value is important because each new term of a Keith sequence is formed by adding the previous d terms. For a three-digit number, the previous three terms are added; for a four-digit number, the previous four terms are added.

The array first stores the digits of the number. Since copy % 10 gives the last digit first, the loop starts from index d - 1 and moves backwards to index 0. This preserves the original left-to-right order of the digits in the array.

After storing the digits, the variable i is set to d, which is the first empty position in the array. The while loop generates terms until the new term reaches or crosses the original number. Inside this loop, sum is reset to 0 before each new term is calculated.

The inner for loop adds the previous d array elements using arr[i - j]. The generated sum is then stored at arr[i]. When the loop stops, the program checks whether the generated term is exactly equal to the original number.

For 197: arr[0] = 1, arr[1] = 9, arr[2] = 7 Next term = 1 + 9 + 7 = 17 Next term = 9 + 7 + 17 = 33 Next term = 7 + 17 + 33 = 57 Next term = 17 + 33 + 57 = 107 Next term = 33 + 57 + 107 = 197

Java Program:

Java
/**
* The class Keith accepts a number and checks whether it is a Keith Number.
* A Keith sequence starts with the digits of the number and each next term
* is the sum of the previous d terms, where d is the number of digits.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Keith
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        /*
        * The original number is preserved in n.
        * copy is used for extracting digits by repeated division.
        */
        int copy = n;

        /*
        * The number of digits tells us how many previous terms
        * must be added to generate each new Keith sequence term.
        */
        String s = Integer.toString(n);
        int d = s.length();

        /*
        * The array stores the original digits first, followed by
        * the generated terms of the Keith sequence.
        */
        int arr[] = new int[n + 1];

        /*
        * Digits are obtained from right to left using % 10.
        * Therefore, they are stored from index d - 1 down to 0
        * so that the original left-to-right order is maintained.
        */
        for(int i = d - 1; i >= 0; i--)
        {
            arr[i] = copy % 10;
            copy = copy / 10;
        }

        int i = d;
        int sum = 0;

        /*
        * New terms are generated until the sequence reaches
        * or crosses the original number.
        */
        while(sum < n)
        {
            sum = 0;

            /*
            * A Keith sequence term is formed by adding the previous d terms.
            */
            for(int j = 1; j <= d; j++)
            {
                sum = sum + arr[i - j];
            }

            arr[i] = sum;
            i++;
        }

        /*
        * If the generated term is exactly equal to the number,
        * the number appears in its own Keith sequence.
        */
        if(sum == n)
        System.out.println("The number is a Keith Number");
        else
        System.out.println("The number is not a Keith Number");
    }
}

Equivalent Python Program:

Python
n = int(input("Enter a number: "))

# The original number is kept unchanged for final comparison.
copy = n

# The number of digits decides how many previous terms are added.
s = str(n)
d = len(s)

# The list stores the original digits followed by the generated
# Keith sequence terms.
arr = [0] * (n + 1)

# Digits are obtained from right to left using % 10.
# They are stored backwards in the list to preserve the original order.
for i in range(d - 1, -1, -1):
    arr[i] = copy % 10
    copy = copy // 10

i = d
total = 0

# New terms are generated until the sequence reaches or crosses
# the original number.
while total < n:
    total = 0

    # A Keith sequence term is formed by adding the previous d terms.
    for j in range(1, d + 1):
        total = total + arr[i - j]

    arr[i] = total
    i = i + 1

# If the generated term is equal to the number, it is a Keith Number.
if total == n:
    print("The number is a Keith Number")
else:
    print("The number is not a Keith Number")

Output:

Enter a number: 197 The number is a Keith Number Enter a number: 14 The number is a Keith Number Enter a number: 53 The number is not a Keith Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →