Tuesday, June 6, 2017

Kaprekar number

A Kaprekar number is a number whose square when divided into two parts and such that sum of parts is equal to the original number and none of the parts has value 0. 
Given a number, the task is to check if it is Kaprekar number or not.

Input : n = 13
Output : No
Explanation : 13^2 = 169. Neither 16 + 9 nor 1 + 69 is equal to 13

Input  : n = 297 
Output : Yes
Explanation:  297^2 = 88209 and 88 + 209 is 297

Input  : n = 10
Output : No
Explanation:  10^2 = 100. It is not a Kaprekar number even if sum of 100 + 0 is 100. This is because of the condition that none of the parts should have value 0.

Solution:

n = int(input())
sq = n*n
r = 0
i = 0
while sq:
      r = (sq%10)*(10**i) + r
      sq = int(sq/10)
      i += 1
      if r + sq == n:
           break
s = "Yes" if sq + r == n and r != 0 else "No"
print(s)


Share: