Skip to main content
LeetCode 258, Easy. Topics: Math, Simulation, Number Theory. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 17 parametrized pytest cases, and a playground notebook:

Problem

Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Examples

Constraints

  • 0 <= num <= 2^31 - 1
Follow up: Could you do it without any loop/recursion in O(1) runtime?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026