Problem overview
The function digitSum takes a non-negative Int, n, and returns the sum of its digits. For n = 472 the digits are 4, 7 and 2, and the sum is 13. For n = 1005 the answer is 6, because the zeros add nothing, and for n = 7 it is 7. When n is 0 the sum is 0.
Approach
You can't index into a number the way you index into a string, so the digits have to be taken off arithmetically. Two operations do it:
num % 10is the remainder after dividing by 10, which is the last digit:472 % 10is 2.num / 10on twoIntvalues drops the remainder, which removes the last digit:472 / 10is 47.
Repeat both until nothing is left. Each turn of a loop adds one digit to a running total and then removes it from the number.
Step by step
- Copy
nintovar num. Function parameters in Scala are immutable, so the loop can't changen, but it can change avar. - Create
var total, starting at 0. Ifnis 0, the loop never runs and 0 is already the right answer. - Write a
whileloop that runs as long asnum > 0. A number with no digits left is 0, which ends the loop. - Inside the loop, add
num % 10tototalwith+=. - Still inside, replace
numwithnum / 10. Without this linenumnever changes and the loop never ends. - After the loop, return
total.
Here is the state after each turn for n = 472:
| Turn | num after the turn | total after the turn |
|---|---|---|
| 1 | 47 | 2 |
| 2 | 4 | 9 |
| 3 | 0 | 13 |
Why the loop must end
Each turn divides num by 10 with the remainder dropped, so it gets smaller every time, and a non-negative Int that keeps shrinking reaches 0. A number with d digits needs exactly d turns, because every division removes exactly one digit. The loop therefore runs once per digit, and the work grows only with the length of the number, not its size: even 1000000000 takes ten turns. The order of the two lines inside the loop matters too: read the digit with % 10 first, then drop it with / 10. Swapping them would add the wrong digit.
The solution in Scala
class Solution {
def digitSum(n: Int): Int = {
var num = n
var total = 0
while (num > 0) {
total += num % 10
num = num / 10
}
return total
}
}
Details that matter in Scala:
vardeclares a variable you can reassign,valone you can't. Bothnumandtotalchange, so both arevar.total += num % 10is shorthand fortotal = total + num % 10. The%operator binds tighter than+, so the remainder is computed first./between twoIntvalues is integer division and truncates. With aDoubleon either side it would keep a fraction and the loop would behave differently.Intis 32 bits and holds up to 2147483647. The lesson's largest input, 1000000000, fits comfortably.
Edge cases and common mistakes
- Zero.
digitSum(0)returns 0, because0 > 0is false and the loop is skipped. - Zeros inside the number. For 1005 the digits taken are 5, 0, 0, 1: the sum is 6.
- Forgetting to shrink
num. If the division line is missing, the same last digit is added forever. - Looping on
num >= 0. This never stops, because0 / 10is 0 again.
Testing
digitSum(472) -> 13
digitSum(1005) -> 6
digitSum(7) -> 7
digitSum(0) -> 0
