Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Sum Digits Until One

Here is the interview question prompt, presented for reference.

We're provided a positive integer num. Can you write a method to repeatedly add all of its digits until the result has only one digit?

Here's an example: if the input was 49, we'd go through the following steps:

// start with 49
4 + 9 = 13

// since the previous addition was 13,
// move onto summing 13's digits
1 + 3 = 4

We would then return 4.

Constraints

  • Input will be in the range between 0 and 1000000000
  • Expected time complexity : O(log n)
  • Expected space complexity : O(1)

You can see the full challenge with visuals at this link.

Challenges • Asked over 5 years ago by Danaila Marian

jch2Xer Commented on Aug 01, 2019:

This is called the digital room problem or This is called the digital **root* problem?Great job on this site by the way, I am enjoying it! thank you!!

Team AlgoDaily Commented on Oct 03, 2019:

Definitely digital root :-) Glad you're enjoying it!

Danaila Marian Commented on Feb 11, 2020:

Hi, guys!
I have a question about "Sum Digits Until One" problem. When I run the code for all the test cases everything is perfect, but when I click 'RUN TESTS' button, it gives me an error:

4
('An error occurred!', NameError("name 'assertEqual' is not defined",))
('An error occurred!', NameError("name 'assertEqual' is not defined",))
('An error occurred!', NameError("name 'assertEqual' is not defined",))

Here is the code:

def sumDigits(n):
if n 0):
s += n % 10
n //= 10
return sumDigits(s)
print(sumDigits(49))

Some thoughts about what is wrong?

Al Commented on May 22, 2020:

Same problem here

Team AlgoDaily Commented on Sep 18, 2020:

This is incorrect. The question states "write a method to REPEATEDLY add all of its digits until the result has only one digit". You can see in the example that we later add 1 + 3 from 13 to get 4.

Anonymous Commented on Sep 18, 2020:

The assertions in the tests for this are incorrect. For example, I am seeing the following: assert sum_digits(49) == 4

It should be sum_digits(49) == 13

ryanmvazquez@gmail.com Commented on Jan 19, 2022:

I think the example here could be made a bit more clear. It isn't obvious from the problem statement and example that all of the digits in the number needed to be added first to provide the next sum.

One could alternatively interpret the problem statement as fibonnaci-esque - deriving the sum from the first digit and the "reduced" sum of the subsequent digits (which could potentially grow infinitely).

The intent could be easily disambiguated by providing an example with > 2 digits.

> for x = 195
> 1 + 9 + 5 = 15
> 1 + 5 = 6

Pat Kumar Commented on Jun 21, 2025:

It’s the digital root problem. The example is correct: 49 -> 4+9=13 -> 1+3=4. Ryan’s suggestion to show a 3-digit example helps disambiguate:
195 -> 1+9+5=15 -> 1+5=6

About the “assertEqual is not defined” error: that’s coming from the site’s test harness, not your function. They’re likely calling unittest’s assertEqual outside a TestCase. Two practical tips to avoid unrelated failures:
- Match the expected function name/signature. Many platforms use snakecase, e.g., sumdigits(n), not sumDigits.
- Don’t print inside the solution; just return the value. Extra prints can confuse runners.

A simple iterative version:

def sum_digits(n):
while n >= 10:
s = 0
while n:
s += n % 10
n //= 10
n = s
return n

O(1) math trick (digital root):

def sum_digits(n):
if n == 0:
return 0
return 1 + (n - 1) % 9

If “Run Tests” still throws assertEqual errors, that needs a fix on the platform side. Similar small-math tricks show up in problems like Find Missing Number in Array, so it’s nice to know both the loop and O(1) versions.

Sarah Chen Commented on Jul 12, 2025:

A few quick notes based on the thread:

  • It’s definitely the digital root problem. The 49 -> 4 example is correct, and the 195 example helps disambiguate the intent.
  • Your recursion is fine logically. The NameError is from the site’s test harness (they’re calling unittest.assertEqual without importing). That’s not on your code. The repeated lines just mean each test crashed before calling your function.

Two things you can do on your side to avoid unrelated failures:
- Match the expected function name/signature (usually snakecase): sumdigits(n), not sumDigits.
- Don’t print in the solution—just return.

Iterative version that most runners accept:

def sum_digits(n):
n = abs(int(n))
while n >= 10:
s = 0
while n:
n, d = divmod(n, 10)
s += d
n = s
return n

O(1) digital root trick (if inputs are non-negative integers):

def sum_digits(n):
n = abs(int(n))
return 0 if n == 0 else 1 + (n - 1) % 9

If “Run Tests” still reports assertEqual is not defined, that needs a fix on the platform side. Also, same snake_case naming convention shows up in problems like Length of String Compression.

Team AlgoDaily Commented on Jul 20, 2025:

Appreciate all the details here—quick updates and clarifications:

  • It is the digital root problem. The example 49 -> 4+9=13 -> 1+3=4 is correct. We’ll add a 3-digit example (e.g., 195 -> 1+9+5=15 -> 1+5=6) to the prompt to remove ambiguity.
  • The “assertEqual is not defined” error was on our side in the test harness. We’ve pushed a fix. Please hard refresh and re-run. If you still see it, let us know your browser + language.
  • Python function signature: use snakecase and return the value (no prints). Expected: sumdigits(n)

Both iterative and O(1) solutions are accepted:
```

O(1) digital root

def sum_digits(n):
return 0 if n == 0 else 1 + (n - 1) % 9
```

Assume non-negative integers for this challenge. If anything else looks off after the fix, reply with the run ID or a screenshot so we can investigate.