Definition
End at 1
Repeated plain digit sums finish at a single 1.
A magic number repeatedly reduces by summing its digits until one digit remains — and that digit is 1. This tutorial covers the definition, digital-root link, a live preview, worked Python examples, edge cases, and complexity.
End at 1
Repeated plain digit sums finish at a single 1.
Magic
19 → 10 → 1.
Root = 1
Magic here means digital root equals 1.
No squares
Happy uses squared digits; magic uses plain sums.
Show steps
Watch each digit-sum reduction in the browser.
n % 9
Digital-root formula after you explain the loops.
A magic number (in this interview sense) is a positive integer that reaches 1 when you repeatedly replace it by the sum of its decimal digits. Example: 19 → 10 → 1.
That final single digit is the digital root. Magic here simply means digital root equals 1 — not the same as happy numbers (which square each digit).
It drills digit peeling and reduction loops — a clean warm-up before (or after) happy / Harshad style problems.
Keep summing digits until n ≤ 9.
Final digit must be exactly 1.
Nested loops or digital-root shortcut.
Reject 0 and negatives by convention here.
In short: keep replacing n with the sum of its digits; if you finish at 1, the number is magic.
Given a positive integer n, decide whether repeated digit-sum reduction ends at 1.
# 19 → 10 → 1 → magic
# 28 → 10 → 1 → magic
# 18 → 9 → not
# 1 → 1 → magic | Item | Type | Description |
|---|---|---|
n | int | Positive integer (reject n ≤ 0). |
| Return / print | bool / text | True if digital root is 1. |
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 | Method | Idea | Notes |
|---|---|---|
| Nested loops | Digit sum until one digit | Interview default — clear and visual |
| Digital root | 1 + (n - 1) % 9 | O(1); mention after the loop story |
| Mod 9 check | n % 9 == 1 (n > 0) | Same shortcut; handle n%9==0 carefully for root 9 |
| Goal | Pattern |
|---|---|
| Last digit | n % 10 |
| Drop digit | n //= 10 |
| One digit-sum pass | total += n % 10 |
| Keep reducing | while n > 9 |
| Magic test | final == 1 |
| Shortcut | 1 + (n - 1) % 9 == 1 |
Same digit idea — different stopping rules and formulas.
sum until 1 digitBeginner-friendly; easy to debug
1+(n-1)%9Constant time after you know the math
sum of squaresDifferent problem — do not mix them
loops firstOffer the % 9 shortcut as a follow-up
Reach for magic-number checks when digit reduction and digital roots appear.
Digit loops with a crisp boolean stop condition.
Same reduction used in divisibility by 9 stories.
Print magic numbers in 1…N for small N.
Contrast plain digit sum vs squared digits vs divisibility.
State that 0 / negatives are out of scope here.
Key benefit: a short digit problem that still opens the door to digital-root theory and O(1) shortcuts.
Enter a positive safe integer to see each digit-sum reduction step.
Three complete Python programs — single check for 19, range 1–50, and a digital-root shortcut. Click View Output to reveal sample console results.
Nested loops that reduce until one digit remains.
19Uses the classic nested-loop digit-sum reduction with a positive-input guard.
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.") The outer loop keeps reducing while the value is multi-digit. The inner loop performs one digit-sum pass. For 19: 1+9=10, then 1+0=1.
Reuse the same helper to filter a beginner interval.
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() Numbers congruent to 1 mod 9 (in this positive range) end at digital root 1. The reusable checker keeps the range loop clean.
Same verdict in O(1) once you know the formula.
Uses 1 + (n - 1) % 9 and compares to 1.
def digital_root(n: int) -> int:
if n <= 0:
raise ValueError("n must be positive")
return 1 + (n - 1) % 9
def is_magic_shortcut(n: int) -> bool:
if n <= 0:
return False
return digital_root(n) == 1
for value in (19, 18, 1, 28):
label = "magic" if is_magic_shortcut(value) else "not magic"
print(f"{value}: {label} (root={digital_root(value)})") Digital root collapses all digit-sum iterations into one modulus. In interviews, explain the loop first, then mention this O(1) shortcut.
Reject n ≤ 0 under this page’s convention.
While n > 9, replace n with its digit sum.
Magic iff the final single digit is 1.
Root 1 → yes; otherwise no.
n = 19Trace the digit-sum reductions for the classic magic example.
| Step | n | Digit sum | Next |
|---|---|---|---|
| 1 | 19 | 1 + 9 | 10 |
| 2 | 10 | 1 + 0 | 1 |
| 3 | 1 | (single digit) | stop — magic |
Contrast: 18 → 9 stops at 9 → not magic.
Where magic-number checks show up beyond the interview prompt.
Digit peeling with a clear stop condition.
Example: write is_magic_number(n).
Bridge loops to the % 9 formula.
Example: root of 19 is 1.
List magic numbers in a classroom interval.
Example: 1 to 50 list above.
Separate magic from happy and Harshad.
Example: plain sum vs squares.
Digital roots relate to rules for 9.
Example: root 9 ↔ multiple of 9.
Print each reduction like the live preview.
Example: 19 → 10 → 1.
Pro Tip: say “magic means digital root is 1” before coding the loops.
Why this pattern works well in interviews and classwork.
One sentence: digit-sum reduction ends at 1.
19 → 10 → 1 is whiteboard-friendly.
Digital-root formula upgrades the solution later.
19 vs 18 and magic vs happy catch misconceptions.
Pro Tip: lead with nested loops; offer the digital-root shortcut if asked about optimization.
Small habits that keep magic-number solutions interview-ready.
Reject n ≤ 0 under this tutorial’s definition.
Use while n > 9, not an arbitrary iteration count.
Magic and not-magic classics catch bugs fast.
That is the happy-number map — different problem.
Explain loops first; then cite digital root.
Pro Tip: 1 is magic because it is already the target single digit — say that when asked about the base case.
Mistakes that commonly break magic-number solutions.
Confusing magic with happy numbers.
→ Sum digits plain — no squares.
Checking only the first digit sum (e.g. 19 → 10) and quitting.
→ Keep going until n ≤ 9.
This page treats them as not magic.
→ Return False for n ≤ 0.
Using n % 9 without the digital-root adjustment.
→ Prefer 1 + (n - 1) % 9 for root values.
Destroying n before you can print the original later.
→ Work on a local copy inside the helper.
Keep input positive and remember magic is not the same as happy.
n = 0This tutorial does not treat 0 as magic.
Reject negatives unless your task defines otherwise.
Happy uses squared digits; this page uses plain digit sum.
Immediately magic — no loop needed.
18 → 9, final digit is not 1.
Expect 1 10 19 28 37 46.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
Quick Takeaway: keep summing digits until one remains; magic means that digit is 1.
| Approach | Time (single n) | Extra space |
|---|---|---|
| Repeated digit-sum loops | O((log n)²) practical small | O(1) |
| Digital-root shortcut | O(1) | O(1) |
Scan [1, N] | O(N) checks | O(1) |
Each digit-sum pass costs O(number of digits); very few passes are needed in practice.
Magic numbers (here) are positive integers whose digital root is 1. Implement the nested digit-sum loops first, test 19 and 18, then mention the O(1) digital-root shortcut.
Practice the three examples above, then continue to matrix addition for a 2D array warm-up.
Final digit 1 means magic; 18 → 9 means not — and never square the digits on this page.
Decide magic the interview-friendly way.
End at 1
DefinitionDigit sum
DigitsDigital root = 1
Math1+(n-1)%9
O(1)No squares
ContrastMagic-number checks in this page are exactly digital-root checks for root = 1.
Learn how to add two matrices element by element in Python.
8 people found this page helpful