- Steps
- 19 -> 10 -> 1
Check Magic Number in Python
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
% 10and// 10. - Simple
ifchecks 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.
18 -> 9 (not magic), 28 -> 10 -> 1 (magic).
Intuition and examples
- Steps
- 28 -> 10 -> 1
- Steps
- 18 -> 9
Takeaway: only the last single digit matters.
Live preview
Enter a positive safe integer to see the same chain logic.
Algorithm
Goal: return true iff repeated digit-sum ends at 1.
Validate
Use positive integer input.
Reduce
While value is multi-digit, replace with digit sum.
Decide
Magic iff final single digit is 1.
📜 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 Check a single number (with explanation)
Uses 19 and the same nested-loop logic as the reference page.
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.
Magic numbers in a range (with explanation)
Prints all magic numbers from 1 to 50.
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
🔄 Input / output examples
| Test number | Typical output line |
|---|---|
| 19 | 19 is a Magic Number. |
| 18 | 18 is not a Magic Number. |
| 1 | 1 is a Magic Number. |
| 28 | 28 is a Magic Number. |
Edge cases and pitfalls
n = 0
This tutorial does not treat 0 as magic.
Sign convention
Reject negatives unless your task defines otherwise.
Magic vs happy
Happy uses squared digits; this page uses plain digit sum.
⏱️ Time and space complexity
| Approach | Time (single n) | Extra space |
|---|---|---|
| Repeated digit-sum loops | O((log n)^2) practical small | O(1) |
| Digital-root shortcut | O(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.
Magic-number checks in this page are exactly digital-root checks for root = 1.
8 people found this page helpful
