Check Magic Number in Python

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digit sum

What you’ll learn

  • The repeated digit-sum definition where final digit must be 1.
  • A clear Python is_magic_number() implementation.
  • Range scanning, live preview, and edge-case handling.

Overview

Magic numbers are based on repeated decimal digit sums. Keep reducing until one digit remains, then test whether that digit is 1.

Two programs

Single check for 19 and range 1 to 50 listing.

Live preview

Shows each reduction step in browser.

Interview depth

Digital root link, edge cases, and complexity.

Prerequisites

Loops, integer division, and remainder.

  • Digit extraction with % 10 and // 10.
  • Simple if checks and function returns.

What is a magic number?

A positive integer is magic if repeated digit-sum reduction ends at 1.

19 -> 10 -> 1, so 19 is magic.

Repeated digit sum

This is equivalent to checking whether digital root is 1.

Contrast

18 -> 9 (not magic), 28 -> 10 -> 1 (magic).

Intuition and examples

19Magic
Steps
19 -> 10 -> 1
28Magic
Steps
28 -> 10 -> 1
18Not magic
Steps
18 -> 9

Takeaway: only the last single digit matters.

Live preview

Enter a positive safe integer to see the same chain logic.

Try 19, 18, or 1.

Live result
Press “Run check” to see the digit-sum steps.

Algorithm

Goal: return true iff repeated digit-sum ends at 1.

1

Validate

Use positive integer input.

2

Reduce

While value is multi-digit, replace with digit sum.

3

Decide

Magic iff final single digit is 1.

📜 Pseudocode

Pseudocode
function is_magic(n): // n > 0
    while n > 9:
        s = 0
        while n > 0:
            s += n mod 10
            n = floor(n / 10)
        n = s
    return n == 1
1

Check a single number (with explanation)

Uses 19 and the same nested-loop logic as the reference page.

python
def is_magic_number(num: int) -> bool:
    if num <= 0:
        return False
    while num > 9:
        total = 0
        while num > 0:
            total += num % 10
            num //= 10
        num = total
    return num == 1

number = 19
if is_magic_number(number):
    print(f"{number} is a Magic Number.")
else:
    print(f"{number} is not a Magic Number.")

Explanation

Outer loop keeps reducing to one digit; inner loop performs one digit-sum pass.

2

Magic numbers in a range (with explanation)

Prints all magic numbers from 1 to 50.

python
def is_magic_number(num: int) -> bool:
    if num <= 0:
        return False
    while num > 9:
        total = 0
        while num > 0:
            total += num % 10
            num //= 10
        num = total
    return num == 1

print("Magic Numbers in the range 1 to 50:")
for i in range(1, 51):
    if is_magic_number(i):
        print(i, end=" ")
print()

Explanation

Reusable checker function keeps range code clean and readable.

Optimization notes

Digital root shortcut: for n > 0, digital root is 1 + (n - 1) % 9.

Interview: explain loop solution first, then mention shortcut.

❓ FAQ

Repeatedly sum decimal digits until one digit remains; if that digit is 1, the number is magic.
Yes. It is already one digit and equals 1.
No. Happy numbers use sum of squared digits. Magic numbers here use plain digit sums.
This page follows interview convention: use positive integers only.
The final one-digit value is digital root. Magic here means digital root = 1.
Each pass sums digits in O(log n); repeated passes are tiny in practice for standard input sizes.

🔄 Input / output examples

Test numberTypical output line
1919 is a Magic Number.
1818 is not a Magic Number.
11 is a Magic Number.
2828 is a Magic Number.

Edge cases and pitfalls

Zero

n = 0

This tutorial does not treat 0 as magic.

Negative

Sign convention

Reject negatives unless your task defines otherwise.

Definition

Magic vs happy

Happy uses squared digits; this page uses plain digit sum.

⏱️ Time and space complexity

ApproachTime (single n)Extra space
Repeated digit-sum loopsO((log n)^2) practical smallO(1)
Digital-root shortcutO(1)O(1)

Summary

  • Definition: repeated digit sum ends at 1.
  • Code: nested loop approach is beginner-friendly.
  • Optional: digital-root formula provides constant-time shortcut.
Did you know?

Magic-number checks in this page are exactly digital-root checks for root = 1.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful