Problem: Kaprekar Number

#Difficulty: Easy

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

Examples

Input :  n = 45  
Output : 1
Explanation : 452 = 2025 and 20 + 25 is 45

Input  : n = 297  
Output : 1
Explanation:  2972 = 88209 and 88 + 209 is 297

Input  : n = 10 
Output : 0
Explanation:  102 = 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.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each line contains an integer N.

Output:
For each test case in a new line print 1 if the no is a Kaprekar number else print 0.

Constraints:
1<=T<=10000
1<=N<=10000

Example:
Input:

2
45
10
Output:
1
0

Implementation: Python2.7

"""
    @author: loctv
    @time: January 2, 2017
"""

def divides_of_square(number):
    """
        Take a number, square it and divide it into two parts
        Return all possible way
    """
    to_string = str(number**2)
    length, result = len(to_string), []
    for i in range(1, length):
        result.append((to_string[:i], to_string[i:]))
    return result

def is_kaprekar_number(parts, original):
    """
        Take two side of number and return if it is kaprekar or not
    """
    for _ in parts:
        left, right = map(int, _)
        if left == 0 or right == 0:
            return False
        if left + right == original:
            return True
    return False if original > 1 else True

if __name__ == '__main__':
    TEST_CASE = input()
    for _ in range(TEST_CASE):
        number = input()
        __ = divides_of_square(number)
        print 1 if is_kaprekar_number(__, number) else 0

Advertisements