← All solutions

Add Digits

May 28, 2025 • Go •math, simulation, number theory • easy

This problem is an example of recursive digit manipulation. The task is to repeatedly sum the digits of a number until only one digit remains. The base case checks whether the number is already a single digit. If so, it’s returned immediately. Otherwise, the function breaks the number into digits using modulo and division, sums them, and calls itself recursively with the result. Recursion here is elegant and performant due to the problem’s inherently logarithmic reduction, each step roughly cuts the number’s magnitude by an order.

Go Solution

func addDigits(num int) int {
  if num < 10 {
    return num
  }

  newNum := 0
  for num > 0 {
    newNum += num % 10
    num /= 10
  }

  return addDigits(newNum)
}
LeetCode Problem Link