Digital Root
One digit
Repeat digit sum until the value is in 0…9.
Condensing a number means repeatedly summing digits until one digit remains — the digital root. This tutorial covers iterative reduction, the mod-9 shortcut, a live preview, worked Python examples, edge cases, and complexity.
One digit
Repeat digit sum until the value is in 0…9.
One pass
Digit sum is one step; condensing may need several.
% 10 // 10
Extract digits in a loop until n ≤ 9.
O(1)
Closed form for nonnegative n using congruence mod 9.
Try any n
Enter a nonnegative integer and see its digital root.
0 & ×9
Handle 0 and multiples of 9 carefully in the formula.
Condensing a number (finding its digital root) means summing decimal digits repeatedly until only one digit remains. Classic chain: 9875 → 29 → 11 → 2.
A one-time digit sum may still be multi-digit. Condensing continues until the value is in 0…9. In base 10, a closed-form shortcut uses congruence modulo 9.
It trains digit extraction, loop design, and a classic modular-arithmetic interview shortcut.
Keep summing until n ≤ 9.
Same answer for nonnegative n in O(1).
Positive multiples map to root 9, not 0.
Digital root of 0 is 0.
In short: sum digits until one remains — or use n % 9 (with the 0 / multiples-of-9 fixes).
Given a nonnegative integer n, return its digital root (single digit after repeated digit sums).
# 9875 → 9+8+7+5 = 29 → 2+9 = 11 → 1+1 = 2 | Item | Type | Description |
|---|---|---|
n | int | Nonnegative integer to condense (define policy for negatives). |
| Return / print | int | Single digit in 0…9 — the digital root. |
function condense(n):
while n > 9:
s = 0
while n > 0:
s += n % 10
n //= 10
n = s
return n | Method | Idea | Notes |
|---|---|---|
| Iterative | Repeat digit sum until n ≤ 9 | Best for showing the process |
| Closed form | n % 9 with 0 / ×9 fixes | O(1) for nonnegative ints |
| Show chain | Record each reduction step | Great interview explanation |
| Goal | Pattern |
|---|---|
| Extract last digit | n % 10 |
| Drop last digit | n //= 10 |
| Outer loop | while n > 9: |
| Closed form | 9 if n % 9 == 0 else n % 9 (n > 0) |
| Alt formula | 1 + (n - 1) % 9 for n > 0 |
| Classic check | 9875 → 2 |
Same family of ideas — different stopping rules and speed.
one pass9875 → 29 only — may still be multi-digit
repeat9875 → 29 → 11 → 2 — final single digit
O(1)Same root via congruence; watch 0 and ×9
loops firstShow iterative method, then derive the shortcut
Reach for digital-root drills when digit loops and mod-9 shortcuts matter.
Checks % / // digit extraction and whether you know the mod-9 trick.
Makes “n ≡ digit sum (mod 9)” concrete.
Digital root 9 means the number is divisible by 9 (if n > 0).
Process digit strings when values exceed fixed integer widths.
If the prompt stops after one sum, that is a different problem.
Key benefit: one short problem that covers digit loops, edge cases, and a clean O(1) modular shortcut.
Enter a nonnegative integer and get its digital root.
Three complete Python programs — iterative reduction, mod-9 closed form, and a reduction-chain display. Click View Output to reveal sample console results.
Reference-style loops — keep reducing until one digit remains.
Outer loop until n ≤ 9; inner loop sums digits with % 10 and // 10.
def condense_number(number: int) -> int:
n = number
while n > 9:
digit_sum = 0
while n > 0:
digit_sum += n % 10
n //= 10
n = digit_sum
return n
number = 9875
print(f"The condensed form of {number} is: {condense_number(number)}") The inner loop accumulates digits into digit_sum; the outer loop assigns that sum back to n until the value is within 0…9.
Same answer for nonnegative inputs in O(1) time.
Special-case 0; for positives divisible by 9 return 9 instead of 0.
def digital_root_nonnegative(n: int) -> int:
if n == 0:
return 0
r = n % 9
return 9 if r == 0 else r
print(f"dr(9875) = {digital_root_nonnegative(9875)} (closed form)")
print(f"dr(999999999999999999) = {digital_root_nonnegative(999999999999999999)} (closed form)") In base 10, n and the sum of its digits are congruent mod 9. Mapping remainder 0 to 9 (when n > 0) matches the iterative digital root.
Print each reduction step for interviews and debugging.
Return the digital root and record every intermediate sum.
def condense_with_chain(number: int) -> tuple[int, list[int]]:
n = number
chain = [n]
while n > 9:
digit_sum = 0
while n > 0:
digit_sum += n % 10
n //= 10
n = digit_sum
chain.append(n)
return n, chain
root, steps = condense_with_chain(9875)
print(" → ".join(str(x) for x in steps))
print(f"Digital root: {root}")
print(f"Also: 1 + (9875 - 1) % 9 = {1 + (9875 - 1) % 9}") Same iterative logic, but each assignment to n is appended to chain. The alternate formula 1 + (n - 1) % 9 matches for positive n.
If already ≤ 9, you are done.
Peel digits with % 10 / // 10 into a running total.
Set n to that sum; continue while n > 9.
The final single digit — equivalently n % 9 with edge fixes.
9875Trace iterative digit sums until one digit remains.
| Step | Current n | Digit sum | Next |
|---|---|---|---|
| 1 | 9875 | 9+8+7+5 = 29 | 29 |
| 2 | 29 | 2+9 = 11 | 11 |
| 3 | 11 | 1+1 = 2 | 2 |
| Done | 2 | — | root = 2 |
Check: 9875 % 9 = 2 — matches the iterative chain.
Where digital-root / condense problems show up beyond the interview prompt.
Digit loops plus an optional O(1) formula.
Example: write condense_number(n).
Shows why digit sums preserve remainder mod 9.
Example: chalkboard 9875 ≡ 2.
Root 9 (n > 0) means n is divisible by 9.
Example: quick check for 18, 27, 36.
Many “reduce to one digit” puzzles are digital roots.
Example: birthday digit reductions.
Sum digits of a string, then condense the total.
Example: 1000-digit input as text.
Contrast digit loops with O(1) closed form.
Example: “why mod 9?”
Pro Tip: in interviews, walk 9875 on the board first — then surprise with the mod-9 one-liner.
Why this pattern works well in interviews and classwork.
Each digit-sum step is easy to demonstrate on paper.
Mod 9 gives the same answer without nested loops.
A few integers suffice — O(1) extra space.
0, multiples of 9, and negatives give structured follow-ups.
Pro Tip: memorize both 9 if r == 0 else r and 1 + (n - 1) % 9 — interviewers may ask for either.
Small habits that keep digital-root solutions interview-ready.
Show the digit-sum loop before the mod-9 shortcut.
Never return 0 for positive n divisible by 9.
Digital root of 0 is 0 — handle it before n % 9.
Assert the result is 2 — a fast golden test.
Say whether you reject or take abs(n).
Pro Tip: for digit strings, sum digit chars mod 9 as you go — you never need the full integer.
Mistakes that commonly break digital-root solutions.
18 % 9 == 0 but the digital root is 9.
→ Map remainder 0 to 9 when n > 0.
9875 → 29 is not yet condensed.
→ Repeat until a single digit remains.
Naive n % 9 for 0 can be mishandled depending on formula.
→ Return 0 explicitly when n == 0.
Condensing is about digit values, not how many digits exist.
→ Sum digits; do not return the length.
Language-specific % behavior differs for negatives.
→ Document abs() or reject negatives.
Check these inputs before calling the solution done.
Digital root is 0.
Root is 9, not 0.
Already condensed — return unchanged.
Use abs(n) or reject input explicitly.
Use string-based digit processing if needed.
Root is 9 — good closed-form check.
Handy follow-ups interviewers sometimes ask.
1 + (n - 1) % 9.Try these variations to lock in the pattern.
mod 9 shortcut for nonnegative inputs.0, multiples of 9, and negative-input policy.Quick Takeaway: keep summing digits until one remains — or use n % 9 with the 0 / multiples-of-9 fixes.
| Approach | Time | Extra space |
|---|---|---|
| Iterative digit reduction | Small digit loops (~O(log n) per pass) | O(1) |
| Closed form (mod 9) | O(1) | O(1) |
| String-based huge numbers | O(d) in digit count d | O(1) (+ output chain) |
For string-based very large numbers, each pass is linear in the number of digits.
Condensing a number is the digital-root problem: sum digits until one remains. Master the iterative loop first, then the mod-9 closed form and its edge cases.
Practice the three examples above, then continue to cube numbers for another classic number-property warm-up.
Handle 0 and multiples of 9 carefully, and explain why mod 9 works in base 10.
Find the digital root the interview-friendly way.
Repeat digit sums
DefinitionUse mod 9
MathRoot is 0
GuardRoot is 9
EdgeClosed form O(1)
AnalysisFor positive numbers, digital root follows 1 + (n - 1) % 9.
Learn how to check whether an integer is a perfect cube with integer roots and edge cases.
9 people found this page helpful