Adam Number Program in Python

Write an iterative/recursive function in Python to check whether the number taken as function argument is Adam Number or not.

Definition: Adam Number is a number which when reversed, the square of the number and the square of the reversed number should be numbers which are which are reverse of each other.

Examples: Adam Numbers up to 1000 are 0, 1, 2, 3, 11, 12, 13, 21, 22, 31, 101, 102, 103, 111, 112, 113, 121, 122, 201, 202, 211, 212, 221, 301, 311.

13 is Adam Number because 132 = 169.
Reverse of 169 = 961.
√961 = 31, which is the reverse of the original number 13.

def isAdam(num):
    n = num
    rev = 0
    while n != 0:
        rev = rev * 10 + n % 10
        n = n // 10
    sn = num ** 2
    sr = rev ** 2
    n = sr
    rev = 0
    while n != 0:
        rev = rev * 10 + n % 10
        n = n // 10
    return sn == rev

num = int(input("Enter the number: "))
if isAdam(num):
    print(str(num) + " is an Adam Number.")
else:
    print(str(num) + " is NOT an Adam Number.")

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.