Digital Root via Modular Arithmetic
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Tests understanding of modular arithmetic by computing the digital root of a large integer without repeatedly summing digits in a loop.
Digital Root of a Large Integer
The digital root of a non-negative integer is obtained by repeatedly replacing the number with the sum of its digits until a single digit (0–9) remains.
For example, starting with 9875:
- 9 + 8 + 7 + 5 = 29
- 2 + 9 = 11
- 1 + 1 = 2
So the digital root of 9875 is 2.
There is a well-known closed-form formula that computes this in O(1) using modular arithmetic — your task is to implement it.
Given a non-negative integer n, return its digital root.
Note: Your solution must not use any digit-by-digit iteration or string conversion. It must use the modular arithmetic formula directly.
(For grading purposes, your function is judged on its return value only — the correct closed-form formula naturally satisfies the no-iteration requirement, and the hidden test cases will verify correctness.)
Constraints
0 <= n <= 10^18- The function must return a single integer in the range
[0, 9]. - For
n = 0, the digital root is0.
Examples
Example 1
Input: digital_root(9875)
Output: 2
Example 2
Input: digital_root(0)
Output: 0
Example 3
Input: digital_root(18)
Output: 9
Hints
Hint 1
The digital root of any positive integer n equals 1 + ((n − 1) mod 9). Think about why numbers cycle through 1–9 repeatedly under digit-sum reduction.
Hint 2
The only special case is n = 0, which should return 0 rather than 9.
Hint 3
This formula works because the digit sum of n has the same remainder mod 9 as n itself — so repeated digit-summing always converges to the value n mod 9 (adjusted so the result is never 0 for positive n).
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Digital Root and Mod 9
The digital root exploits a fundamental property of our base-10 positional numeral system: any integer n and the sum of its digits share the same remainder when divided by 9.
Why? Each digit position contributes d * 10^k. Since 10 ≡ 1 (mod 9), we have 10^k ≡ 1 (mod 9) for all k ≥ 0. Therefore:
n ≡ d_m * 10^m + ... + d_1 * 10 + d_0
≡ d_m + ... + d_1 + d_0 (mod 9)
So digit-summing preserves the residue mod 9. Since repeated digit-summing eventually reaches a single digit, that digit equals n mod 9 — except that mod 9 maps multiples of 9 to 0, while the digital root of any positive multiple of 9 is 9 (not 0).
The closed-form that handles this:
digital_root(n) = 0 if n == 0
= 1 + (n - 1) mod 9 if n > 0
The shift of n - 1 maps the range [1..9] → [0..8] so that mod 9 gives 0..8, and adding 1 back gives 1..9. This neatly handles multiples of 9 returning 9 instead of 0.
Correctness
n = 0: returns 0 by the base case.n = 9:1 + (9-1) % 9 = 1 + 8 = 9. Correct.n = 18:1 + 17 % 9 = 1 + 8 = 9. Correct (1+8=9).n = 19:1 + 18 % 9 = 1 + 0 = 1. Correct (1+9=10, 1+0=1).- Works for all
nup to 10^18 in O(1) time with no iteration.
Complexity
- Time: O(1) — single arithmetic operation.
- Space: O(1) — no additional storage.
Reference Solution
def digital_root(n: int) -> int:
if n == 0:
return 0
return 1 + (n - 1) % 9