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 checks if n is a magic number will end when n becomes 0 and sum becomes single digit when n becomes 0 but we have a total_sum we update the value of n with the value of the sum digits only when sum of digits isn t single digit return true if sum becomes 1
def magic_number(n):
    
    total_sum = 0

    
    
    
    while n > 0 or total_sum > 9:
        
        
        if n == 0:
            n = total_sum  
            total_sum = 0
        total_sum += n % 10
        n //= 10

    
    return total_sum == 1