Hackerrank Modified Kaprekar Numbers Solution

This is the solution to the Modified Kaprekar Numbers found in the implementation section of the algorithm domain in Hackerrank. A modified Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n).

Screenshot


The Code
def sendKap(x,s):
 y=x*x
 y=str(y)
 s1=y[:len(y)/2:]
 s2=y[len(y)/2::]
 s3=int(s1)+int(s2)
 if(s3==x):
  s.append(str(x))
st=input()
en=input()
s=[]
for i in range(st,en+1):
 if(i>9):
  sendKap(i,s)
 elif(i==1):
  s.append(str(i))
 elif(i==9):
  s.append(str(i))
if(len(s)==0):
 print 'INVALID RANGE'
else:
 print ' '.join(s)
Got any problems feel free to comment them here !

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Employee Management System Using Inheritance in Java

Bit Stuffing Code Implementation in Java