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 JavaScript 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 | number | Nonnegative integer to condense (define policy for negatives). |
| Return / print | number | Single digit in 0…9 — the digital root. |
function condense(n):
while n > 9:
s = 0
while n > 0:
s += n % 10
n = floor(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 = Math.floor(n / 10) |
| Outer loop | while (n > 9) |
| Closed form | r === 0 ? 9 : r (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 JavaScript programs with Try it Yourself editors — 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 Math.floor(n / 10).
function condenseNumber(number) {
let n = number;
while (n > 9) {
let digitSum = 0;
while (n > 0) {
digitSum += n % 10;
n = Math.floor(n / 10);
}
n = digitSum;
}
return n;
}
const number = 9875;
console.log("The condensed form of " + number + " is: " + condenseNumber(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.
function digitalRootNonnegative(n) {
if (n === 0) {
return 0;
}
const r = n % 9;
return r === 0 ? 9 : r;
}
function digitalRootBigInt(n) {
if (n === 0n) {
return 0;
}
const r = n % 9n;
return r === 0n ? 9 : Number(r);
}
console.log("dr(9875) = " + digitalRootNonnegative(9875) + " (closed form)");
console.log("dr(999999999999999999) = " + digitalRootBigInt(999999999999999999n) + " (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.
function condenseWithChain(number) {
let n = number;
const chain = [n];
while (n > 9) {
let digitSum = 0;
while (n > 0) {
digitSum += n % 10;
n = Math.floor(n / 10);
}
n = digitSum;
chain.push(n);
}
return { root: n, chain };
}
const { root, chain } = condenseWithChain(9875);
console.log(chain.join(" → "));
console.log("Digital root: " + root);
console.log("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 condenseNumber(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 r === 0 ? 9 : 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 Math.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 Math.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 Math.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)
AnalysisIn base ten, the digital root (this page’s “condensed” value) is related to divisibility by 9: for n > 0, repeated digit sums eventually match 1 + (n - 1) % 9, with 9 instead of 0 when n is a nonzero multiple of 9.
Learn how to check whether an integer is a perfect cube with integer roots and edge cases.
9 people found this page helpful