Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Last active March 2, 2019 12:36
Show Gist options
  • Save jatinsharrma/bf3d0581a635258520ca93e11722af0c to your computer and use it in GitHub Desktop.
Save jatinsharrma/bf3d0581a635258520ca93e11722af0c to your computer and use it in GitHub Desktop.
Kaprekar number in range
#This program accepts two numbers from user in different lines and calculate all the Kaprekar numbers between entered numbers.
#In mathematics, a non-negative integer is called a "Kaprekar number" for a given base if the representation of its square in that base can be split into two parts that add up to the original number, with the proviso that the part formed from the low-order digits of the square must be non-zero—although it is allowed to include leading zeroes. For instance, 45 is a Kaprekar number, because 452 = 2025 and 20 + 25 = 45. The number 1 is Kaprekar in every base, because 12 = 01 in any base, and 0 + 1 = 1. Kaprekar numbers are named after D. R. Kaprekar.
#-----------------------------------------
#function to calculate Kaprekar number
#-----------------------------------------
def calc(number):
global b
given = number
a= str(number*number)
number= str(number)
a = int(a[:len(a)-len(number)])+int(a[len(a)-len(number):])
#print (str(a)+"----------------"+str(number))
if given == a:
b+=1
print (int(number), end=" ")
#-----------------------------------------
# main function
#-----------------------------------------
def core(n,num):
for i in range(n,num+1):
if i == 1:
print (i, end=" ")
elif i > 8 :
calc(i)
#-----------------------------------------
#variable to check invalid range
#-----------------------------------------
b= 0
#-----------------------------------------
# Taking user input
#-----------------------------------------
n= int(input())
num = int(input())
#------------------------------------------
#calling core function
#------------------------------------------
core(n,num)
#------------------------------------------
#handling if Invalid range
#------------------------------------------
if b == 0:
print ("INVALID RANGE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment