Problem overview
You are given a non-negative integer n with at least two digits. The function lastTwoDigitSum returns the sum of its last two digits.
For n = 2379, the last two digits are 7 and 9, and 7 + 9 = 16.
Approach
You could turn the number into text and pick out characters, but arithmetic is simpler. Two operators do the work:
- The remainder operator
%.n % 10is the last digit ofn, andn % 100is the number formed by its last two digits. - Integer division
/. When both operands are integers, Go drops the fractional part, so dividing a two-digit number by10gives its first digit.
For 2379: 2379 % 100 is 79. Then 79 % 10 is 9 and 79 / 10 is 7. Add them to get 16.
Working on lastTwoDigits rather than on n keeps the later steps simple: the value is always between 0 and 99, so one division by 10 is enough to separate its two digits, no matter how long n is.
Step by step
- Isolate the last two digits. Declare
lastTwoDigits := n % 100. Everything above the tens place is gone, and the value is between 0 and 99. - Take the last digit.
lastDigit := lastTwoDigits % 10is the ones digit of79, which is9. - Take the second-to-last digit.
secondLastDigit := lastTwoDigits / 10. Dividing by 10 discards the ones digit, and integer division keeps just the whole part:79 / 10is7. - Add and return. The two digits are separate numbers now, so a plain
+sums them.
The solution in Go
func lastTwoDigitSum(n int) int {
lastTwoDigits := n % 100
lastDigit := lastTwoDigits % 10
secondLastDigit := lastTwoDigits / 10
return lastDigit + secondLastDigit
}
Some Go details behind these lines:
:=declares and infers. Each variable is anint, becausenis anintand so are the constants.- Integer division truncates toward zero. For non-negative numbers, like the ones in this task, that is the same as rounding down. For negative numbers Go behaves differently from some other languages:
-7 / 2is-3and-7 % 2is-1, since the remainder takes the sign of the dividend. The task promises a non-negativen, so you do not have to handle that. - No conversion to a string. Working with numbers avoids slow and fiddly string handling.
Trace one more value through the code, n = 1005. First, 1005 % 100 is 5, so lastTwoDigits is 5. Next, 5 % 10 is 5, which becomes lastDigit. Then 5 / 10 is 0, which becomes secondLastDigit. The sum is 5. The tens digit really is zero, and the code handles that without any special case.
The same result also fits on one line, as n%10 + n/10%10. Splitting it into named steps, as the lesson does, makes each step easy to read and to check.
Complexity
The function does a fixed number of operations, so time is O(1) and space is O(1). The number of digits in n does not matter, because % reads only the end of the number.
Edge cases and common mistakes
- Exactly two digits.
lastTwoDigitSum(45)gives9:45 % 100is45, the digits are 5 and 4. - Zeros in the last two digits.
lastTwoDigitSum(100)gives0, andlastTwoDigitSum(1005)gives5. HerelastTwoDigitsis5, sosecondLastDigitis0, which is correct: the tens digit really is zero. - Mixing up
%and/.n / 100removes the last two digits instead of keeping them. - Dividing the original
nby 10.n / 10is237for2379, not a digit. DividelastTwoDigits, which is always below 100. - Unused variables. If you declare a variable and do not use it, Go refuses to compile.
Small inputs to try: lastTwoDigitSum(2379) gives 16, lastTwoDigitSum(45) gives 9, lastTwoDigitSum(100) gives 0 and lastTwoDigitSum(1005) gives 5.