Skip to content
RED SAGSkills
easy15 ptsmath

Repeated Digit Sum

Take a non-negative integer n. Replace it with the sum of its decimal digits, and keep doing so until a single digit remains. Return that digit.

Can you find an O(1) formula?

Example 1

Input: n = 9875
Output: 2
Explanation: 9+8+7+5 = 29, 2+9 = 11, 1+1 = 2.

Example 2

Input: n = 0
Output: 0

Constraints

  • 0 <= n <= 2^31 - 1
Hint 1

A number and the sum of its digits leave the same remainder when divided by 9.

Hint 2

Handle 0 and multiples of 9 carefully.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Game Developer, Automation Test Engineer, Embedded Systems Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.