Skip to main content

Keith Number

KEITH NUMBER


Before  moving on to code let us talk about what is Keith number . A number is said to be Keith Number, if  it appears in the sequence where first n numbers are digits of  number itself and rest are formed by repeatedly adding the previous n numbers of the sequence . It is illustrated in the example below:

eg. 
1. 14 is a Keith number    
    Sequence formed by 14 is  1, 4, 5, 9, 14, . . . . 
    First two numbers(1, 4) are digits of number(14)  itself and rest are formed by adding the  previous  two numbers of sequence(1+4=5, 5+4=9, 9+5=14). 

2. 197 is a Keith number
    Sequence formed by 197 is 1, 9, 7, 17,  33,  57, 107, 197, . . . .
    First three numbers(1, 9, 7) are digits of number(197)  itself and rest are formed by adding the  previous  two numbers of sequence(1+9+7=17, 9+7+17=33, 7+17+33=57 ,17+33+57=107 , 33+57+107-=197). 

NOTE:t is recommended that you first try this question by yourself.

Python Code

# Program to check for  keith number

t=int(input("Enter the number of times you want to execute the program "))
#This loop is for the number of times you want to execute the program.. You don't have to restart the program again and again
for _ in range(t):
n=input("Enter a number to be checked for a Keith number ")
length=len(n)
lst=[]
# Appending the digits of number to a list
for i in n:
lst.append(int(i))


sum=0
c=0

# We will break the loop when sum of the last n numbers of seequence will be greater than n itself
while sum<int(n):
#There is no keith number from 1 to 10
if int(n)>=0 and int(n)<=10:
break

# Adding the last n numbers from the sequence
sum=0
for i in range(len(lst),len(lst)-length,-1):
sum+=lst[i-1]
lst.append(sum)

# If sum is equal to n then break the loop and initialize c to 1
if sum==int(n):
c=1
break
if c==1:
print(f"{n} is a Keith number")
else:
print(f"{n} is a not Keith number")
Output:
Enter the number of times you want to execute the program 6
Enter a number to be checked for a Keith number 14
14 is a Keith number
Enter a number to be checked for a Keith number 12
12 is a not Keith number
Enter a number to be checked for a Keith number 5752090994058710841670361653731519
5752090994058710841670361653731519 is a Keith number
Enter a number to be checked for a Keith number 197
197 is a Keith number
Enter a number to be checked for a Keith number 47
47 is a Keith number
Enter a number to be checked for a Keith number 565
565 is a not Keith number

Please share this post if  this was useful and comment below your doubts and questions

Comments

Popular posts from this blog

How to install python IDE ? IDLE and Pycharm

How to install Python IDE on Windows 10(Pycharm and IDLE)? Pycharm is the best Python IDE available.It comes with many in -built features. We will discuss its shortcuts in our next blogs.Pycharm is a cross-platform editor developed by jetbrains.   Installing Python Step 1) Visit the official website of python or click the below link              https://www.python.org/downloads/  .  Step 2) Click on Download Python               Step 3) After the download is complete ,open the exe file and click               on Install now  Step 4) After the successful installation it will show you a message         Setup was successful Step 5) Now go to your windows search bar and type "IDLE"              Press Enter and you will see that idle is opened Installing Pycharm St...

String Slicing in python

String Slicing Python  Slicing refers to extracting some words from a string. As in many other languages there are many different concepts for extracting a letters or words from a string ,in python we are having the concept of slicing. We can also the same things using looping, we will learn about it in our next tutorials. Indexing in Strings Before starting slicing, we need to understand the indexing in python. We have the concept of positive indexing  and  negative indexing.  Positive indexing starts from 0 to (length of string).It goes from forward to backward. Negative indexing starts from -1 to -(length of string + 1). It is illustrated in below example Below are some examples of how to use string slicing to extract characters from string. Accessing the characters in a string using positive index eg. string= "We are studying string slicing" s=string[ 0 ] # A single character will be printed present at index 0 w=string[ 4 ] # A...