Keith Number in JavaIn this section, we will learn what is Keith number and also create Java programs to check if the given number is Keith or not. The Keith number program frequently asked in Java coding test. Keith NumberA positive n digit number X is called a Keith number (or repfigit number) if it is arranged in a special number sequence generated using its digits. The special sequence has first n terms as digits of x and other terms are recursively evaluated as the sum of previous n terms. For example, 197, 19, 742, 1537, etc. Keith Number ExampleLet's check the number 742 is a Keith number or not. First, we will separate each digit, as 7, 4, 2 To find the next term of the abovecreated series, we add these digits (i.e. 7+4+2), and the resultant (13) that we get becomes the next term of the series. Now, the series becomes, 7, 4, 2, 13 To find the next term of the above series, we add the last three terms (i.e. 13+2+4), and the resultant (19) that we get becomes the next term of the series. Now, the series becomes, 7, 4, 2, 13, 19 To find the next term of the above series, we add the last three terms (i.e. 19+13+2), and the resultant (34) that we get becomes the next term of the series. Now, the series becomes, 7, 4, 2, 13, 19, 34 To find the next term of the above series, we add the last three terms (i.e. 34+19+13), and the resultant (66) that we get becomes the next term of the series. Now, the series becomes, 7, 4, 2, 13, 19, 34, 66 To find the next term of the above series, we add the last three terms (i.e. 66+34+19), and the resultant (119) that we get becomes the next term of the series. Now, the series becomes, 7, 4, 2, 13, 19, 34, 66, 119 To find the next term of the above series, we add the last three terms (i.e. 119+66+34), and the resultant (219) that we get becomes the next term of the series. Now, the series becomes, 7, 4, 2, 13, 19, 34, 66, 119, 219 To find the next term of the above series, we add the last three terms (i.e. 219+119+66), and the resultant (404) that we get becomes the next term of the series. Now, the series becomes, 7, 4, 2, 13, 19, 34, 66, 119, 219, 404 To find the next term of the above series, we add the last three terms (i.e. 404+219+119), and the resultant (742) that we get becomes the next term of the series. Now, the series becomes, 7, 4, 2, 13, 19, 34, 66, 119, 219, 404, 742 Here, we will stop the process because we get the same number (742) as a term of the series. Hence, the given number 742 is a Keith number. From the above example, we observe that we need to calculate the terms of the series until we get the same number (that we have taken in starting) as a term of the series. Note: If the given number (X) has n number of digits, we will recursively add the last n terms of the series. As the number 742 has three digits, so, we have added the last three terms of the series, each time.Let's take another example. Steps to Find Keith Number
Let's implement the above steps in a Java program. Keith Number Java ProgramThe logic is not much difficult. Compute the series using the sum of n previous numbers where n is the number of digits. If one of the numbers computed in the series is the same as the input number, it is a Keith number. The program stops if the value computed is greater than the input number. Let's create a Java program and implement the above logic into it. KeithNumberExample1.java Output: Yes, the given number is a Keith number. Yes, the given number is a Keith number. No, the given number is not a Keith number. Let's create another Java program to find all the Keith numbers that contain the same number of digits. KeithNumberExample2.java Output 1: Output 2: Similarly, we can find the ddigit Keith numbers. The following table summarizes the ddigits Keith numbers.
Next TopicNeon Number in Java
