How to display magic numbers in Python?

In the program example below, we display magic numbers between 1 and 200.

Example 1 (Easy Approach)

# Python program to print Magic numbers between 1 and 200.
start = 1
end = 200
list_magic = []
for numb in range(start,end+1):
    if numb % 9 == 1:
        list_magic.append(numb)
print("Magic numbers between 1 and 200: \n",list_magic)

Output:

Magic numbers between 1 and 200: 
 [1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91, 100, 109, 118, 127, 136, 145, 154, 163, 172, 181, 190, 199]

Example 2

# Python program to print Magic numbers between 1 and 200.
start = 1
end = 200
list_magic = []
# define a function to calculate the sum of digits in a number
def sum_digit(x):
    dig_sum = 0
    num = x
    while num > 0:
        dig_sum +=num%10
        num = num//10
    return dig_sum

# iterate from 1 to 200 and check if a number is magic number
# if number is magic number append the list_magic
for numb in range(start,end+1):
    digSum = sum_digit(numb)
    while digSum > 9:
        digSum = sum_digit(digSum)
    if digSum == 1:
        list_magic.append(numb)
print("Magic numbers between 1 and 200: \n",list_magic)

Output:

Magic numbers between 1 and 200: 
 [1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91, 100, 109, 118, 127, 136, 145, 154, 163, 172, 181, 190, 199]

What is a Magic Number?

A number is said to be a magic number if the sum of digits of the number recursively equals to 1.

For example, 
  • 172 is a magic number. As the sum of digits 1+7+2 = 10 and again 1+0 = 1. 
  • Any combination of digits of a magic number is also a magic number. Such as 712,721, 271, 217, 127 are also magic numbers.

Easy Method to check if a number is magic number -
number % 9 == 1

If remainder  = 1 ==> number is a magic  number.

Instead of adding the digits recursively to check if the number is magic number, we can just find the remainder when number is divided by 9. 

If remainder is 1 then the recursive sum of digits is 1 so the number is a magic number. 

See the below explanation.

235 % 9 = 1 
=> 2+3+5=10 
=> 1+0=1 
=> So a magic number. 

Steps to Display Magic Numbers

Approach 1 (Easy)

  • Step 1 - Define start and end of the range. 
  • Step 2 - Define a List list to store the magic numbers.
  • Step 3 - Loop over the numbers between the range
    • Step 3.1 - Check if remainder is 1 when number is divided by 9. Append the list.
  • Step 4 - Check if length of the list is zero. Magic Number between the range.
    • Print the List of magic numbers.
We followed the above approach in Example 1.

Approach 2

  • Step 1 - Define start and end of the range. 
  • Step 2 - Define a List list  to store the magic numbers.
  • Step 3 - Define function sum_digit() to find sum of digits.
  • Step 3 - Loop over the numbers between the range
    • Step 3.1 - Compute sum of digits. 
    • Step 3.2 - Check if sum of digits is greater than 9. Repeat step 3.1.
    • Step 3.3 - Check if sum of digits is 1. Append the list.
  • Step 4 - Check if length of the list is zero. Magic Number between the range.
    • Print the List of magic numbers.
We followed the above approach in Example 2.

Example 3 (Taking User Input)

In the program below, first we take start and end of the range and then follow approach 1 to display magic numbers between start and end.
# Python program to print Magic numbers
print("Enter the range:")
start = int(input("Start: "))
end = int(input("End: "))
list_magic = []

for numb in range(start,end+1):
    if numb % 9 == 1:
        list_magic.append(numb)
if len(list_magic) == 0:
    print(f"No Magic Number between {start} and {end}.")
else:
    print(f"Magic numbers between {start} and {end}: \n",list_magic)

Output:

Enter the range: Start: 3 End: 34 Magic numbers between 3 and 34: [10, 19, 28]

FAQ

How to check if a number is magic number?

Answer

# Python program to check if a number is magic number
num = int(input("Enter the number: "))

def magic_number(num):

    if num % 9 == 1:
        print(f"{num} is a magic number.")
    else:
        print(f"{num} is not a magic number.")

magic_number(num)

Output:

Enter the number: 897


Comments