Checking for Magic Numbers using Functions in Python

In this Python tutorial, we shall see how to check whether a number is a Magic Number or not. We shall use functions in Python for the same.

What is a Magic Number?

If we iterate the process of summing the digits of a number and then consider the sum as the new number, hence repeating the process till we are left with a single digit. If the digit left over is 1, then the original number is called a magic number.

Let us look at a few examples to understand what a Magic Number is:-

  • Consider the number 55. The sum of the digits of 55 is (5+5) = 10. The sum of the digits of 10 is (1+0) = 1. Since the process is terminating with 1 as the last single digit, 55 is considered to be a magic number.
  • Consider the number 1234. The sum of the digits of 1234 is (1+2+3+4) = 10. The sum of the digits of 10 is (1+0) = 1. Since the process is terminating with 1 as the last single digit, 1234 is considered to be a magic number.
  • Consider the number 12345. The sum of the digits of 12345 is (1+2+3+4+5) = 15. The sum of the digits of 15 is (1+5) = 6. Since the process is not terminating with 1 as the last single digit, 12345 is not considered to be a magic number.
  • Consider the number 183. The sum of the digits of 183 is (1+8+3) = 12. The sum of the digits of 12 is (1+2) = 3. Since the process is not terminating with 1 as the last single digit, 183 is not considered to be a magic number

Functions in Python

Function is a block of code that is executed when it is called.

Why use functions in Python?

  • Functions help reduce the complexity of the code
  • It simplifies the interface of the code
  • Code reusability increases as a function can be called multiple times.

In Python, functions are defined with the keyword def and return type of the function need not be mentioned.

Let us see an example of functions in Python from the following code:-

#This is a function called 'sum' which calculates the sum of two numbers
def sum(a,b):
    sum = a+b
    return sum

#Printing what the function 'sum' is returning    
print(sum(2,2))
print(sum(4,2))

Output:-

4
6

Here, as you can see:-

  • We are defining the function with the def keyword.
  • The variables a and b are passed as arguments to this function.
  • The function sum returns the sum of the arguments passed

Python Code to check if a given number is a Magic Number

Let us look at the following Python Code:-

#Function to calculate the number of digits in a number
def digCount(num):
    c = 0
    while num != 0:
        num = num//10
        c += 1
    return c
#Function to calculate the sum of digits of a number
def digSum(num):
    temp = num
    sum = 0
    for i in range(digCount(num)):
        sum+=num%10
        num//=10
    return sum
#Function to check whether a number is a magic number or not
def magic_check(num):
    while(digCount(num)>1):
        num = digSum(num)
    if num == 1:
        return True
    else:
        return False
#Initialising a list with some numbers
numbers = [1234,12345,12,19, 55, 555]
#Iterating over the list and using the magic_check function
for i in numbers:
    if(magic_check(i)):
        print(f"{i} IS a Magic Number")
    else:
        print(f"{i} IS NOT a Magic Number")

The output for the given Python code is:-

1234 IS a Magic Number
12345 IS NOT a Magic Number
12 IS NOT a Magic Number
19 IS a Magic Number
55 IS a Magic Number
555 IS NOT a Magic Number

Try to dry run the following code before moving on the explanation of the code.

Explanation of the functions included in the Python code

–> digCount(num):-

In the digCount function, we aim to calculate the number of digits of the argument (num) that is passed.

  • Initialise a counter to count the number of digits and set it at 0.
  • Run a while loop with the exit condition that the loop will run as long as num is not equal to 0.
  • Initialise a variable (c) to 0. We shall count the digits here.
  • Floor divide num by 10.¬† Floor division is when we divide a number and truncate the decimal part that would remain.
  • Add 1 to c
  • After the iteration is complete, we return ‘c’, which contains the value for the number of digits that the argument has.

–> digSum(num):-

In the digCount function, we aim to calculate the sum of digits of the argument (num) that is passed.

  • Initialise a counter to count the number of digits and set it at 0.
  • Run a for loop that will run for the same number of times as the number of digits in num. digCount(num) obtains this number.
  • Initialise a variable (sum) to 0. We shall count the digits here.
  • We use the Modulo Operator which¬†returns the remainder left when the operand on the left side of the operator is divided by the operand on it’s right. For example: 10%3 = 1
  • Using the Modulo operator, we extract the last digit from num and add it to sum
  • Floor divide num by 10, hence eliminating the last digit that had been added to sum.
  • Return ‘sum’ which contains the sum of the digits of num.

–>magic_check(num):-

In the magic_check function, we aim to check whether the argument passed (num) is a magic number or not.

  • We initialise a variable called temp, where we store the value of num
  • Run a while loop which runs while the digit count of the number in question (num) is greater than 1.
  • Set the value of num as the sum of its digits.
  • After exiting the loop, check if the value of num is 1 or not.
  • If the value is 1, we return True else we return False.

I hope this Python tutorial was helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *