Problem overview
The function digitSum receives a non-negative integer n and returns the sum of its digits. For n = 472 the digits are 4, 7 and 2, so the answer is 13. For 1005 it is 6, because the zeros add nothing, and for 7 it is 7. For 0 the sum is 0.
Approach
An integer has no digits to loop over, the way a string has characters. Arithmetic can pull them off one at a time, though, using two operators:
n % 10is the remainder of dividing by 10, which is the last digit:472 % 10is 2.n // 10is integer division, which drops the last digit:472 // 10is 47.
Repeat both until nothing is left. Add each last digit to a running total, then remove that digit. When n reaches 0, every digit has been counted.
Step by step
- Start
totalat 0. No digit has been added yet. This also gives the right answer forn = 0, where the loop never runs. - Loop while
n > 0. Every turn removes one digit, so the loop runs once per digit. - Add
n % 10tototal. That is the current last digit. - Set
nton // 10. Without this linennever changes and the loop never ends. - Return
totalafter the loop.
Here is the loop on n = 472:
| n at the start | n % 10 | total after adding | n after dropping |
|---|---|---|---|
| 472 | 2 | 2 | 47 |
| 47 | 7 | 9 | 4 |
| 4 | 4 | 13 | 0 |
With n at 0 the condition fails and the function returns 13.
The solution in Python
class Solution:
def digitSum(self, n: int) -> int:
total = 0
while n > 0:
total += n % 10
n = n // 10
return total
Details worth knowing:
//is integer division. It floors the result, so47 // 10is 4 and4 // 10is 0. Plain/always gives a float:9 / 2is4.5, and a float would not have a last digit to take.- Reassigning a parameter.
n = n // 10changes only the local namen. The caller's variable is not affected, because integers are immutable. - Big numbers are fine. Python integers don't overflow. For
999999999the function returns 81, and for1000000000it returns 1. - The string shortcut.
sum(int(d) for d in str(472))also gives 13. The loop above uses only arithmetic, which is the point of this lesson.
Why it works
A number such as 472 is really 4 * 100 + 7 * 10 + 2. The remainder after dividing by 10 isolates the part that is not a multiple of 10, which is the last digit, and the integer division keeps everything above it. Since addition doesn't care about order, taking the digits from the right gives the same total as reading them from the left. The loop runs once per digit, so at most ten times for the largest allowed input.
Python can also do both operations in one call: divmod(472, 10) returns (47, 2), the quotient and the remainder. A loop written as n, d = divmod(n, 10) followed by total += d gives 13 for 472, the same as the version above.
Edge cases and common mistakes
- Zero.
digitSum(0)returns 0: the loop condition is false at once. - Zeros inside the number.
1005gives 5 + 0 + 0 + 1 = 6. The zero digits are added, and they change nothing. - Forgetting to shrink
n. If then = n // 10line is missing, or is outside the loop, the program runs forever. - Using
n >= 0. Since0 // 10is 0,nwould never become negative and the loop would never stop. - Returning inside the loop. The function must return after the loop, once all digits are added.
Testing
472gives13.1005gives6.7gives7.0gives0.999999999gives81.1000000000gives1.
