File: add-digits/solution.py

Date: 2026-06-06

Time: 15:12

add-digits/solution.py

Purpose

This file solves LeetCode 258 — Add Digits. It computes the digital root of a non-negative integer: the single digit obtained by repeatedly summing all digits until only one digit remains. For example, 38 → 3+8=11 → 1+1=2.

Key Components

Solution.addDigits(self, num: int) -> int — The sole method. Takes a non-negative integer and returns its digital root (always 0–9).

Patterns

The solution uses the O(1) digital root formula rather than iteratively summing digits:


digital_root(0) = 0
digital_root(n) = 1 + (n - 1) % 9   for n > 0

This is a well-known number theory result. The digital root of any positive integer is its value modulo 9, except when the result would be 0 (i.e., multiples of 9), where the answer is 9 instead. The expression 1 + (n - 1) % 9 encodes both cases in a single formula by shifting the modular arithmetic: it maps n=9 to 9, n=18 to 9, n=1 to 1, etc.

The naive approach — a loop that sums digits until num < 10 — would be O(log n) per iteration with O(log log n) iterations. This formula eliminates all iteration.

Dependencies

Imports: None. Pure computation with no library dependencies.

Imported by: The test_solution.py files listed in the "Imported By" section are a red herring — those are other problems' test files that happen to share a common test harness pattern (importing a Solution class from a sibling solution.py), not actual consumers of this specific addDigits method.

The real consumer is add-digits/test_solution.py.

Flow

1. Guard: if num == 0, return 0 immediately.

2. Otherwise, compute and return 1 + (num - 1) % 9.

No loops, no recursion, no mutation. Single-expression return for the non-zero case.

Invariants

Error Handling

None. The method assumes valid input per the LeetCode contract. No exceptions are raised or caught.

Topics to Explore

Beliefs