Magic Number A number is said to be a magic number, if summing the digits of the number and then recursively repeating this process for the given sum untill the number becomes a single digit number equal to 1. Example: Number  50113  5011310  101 This is a Magic Number Number  1234  123410  101 This is a Magic Number Number  199  19919  1910  101 This is a Magic Number Number  111  1113 This is NOT a Magic Number The following function checks for Magic numbers and returns a Boolean accordingly. Checks if n is a magic number  totalsum  0 will end when n becomes 0 AND sum becomes single digit. while n  0 or totalsum  9: when n becomes 0 but we have a totalsum, we update the value of n with the value of the sum digits if n  0: n  totalsum   only when sum of digits isn't single digit totalsum  0 totalsum  n  10 n  10 Return true if sum becomes 1 return totalsum  1
def magic_number(n):
    """ Checks if n is a magic number """
    total_sum = 0

    # will end when n becomes 0
    # AND
    # sum becomes single digit.
    while n > 0 or total_sum > 9:
        # when n becomes 0 but we have a total_sum,
        # we update the value of n with the value of the sum digits
        if n == 0:
            n = total_sum  # only when sum of digits isn't single digit
            total_sum = 0
        total_sum += n % 10
        n //= 10

    # Return true if sum becomes 1
    return total_sum == 1
