Problem overview
The function last_two_digit_sum receives a non-negative integer n with at least two digits and returns the sum of its last two digits. For n = 2379 the last two digits are 7 and 9, so the answer is 7 + 9 = 16.
No loop and no conversion to text is needed. Two arithmetic operators, % (remainder) and / (division), pull a number apart digit by digit.
Approach
Our number system is base 10, so the remainder of a division by 10 is the last digit, and the remainder of a division by 100 is the number formed by the last two digits:
2379 % 10is9.2379 % 100is79.
Once the problem is reduced to the two-digit number 79, the digits are easy to separate. Its last digit is 79 % 10 = 9. Its first digit is 79 / 10 = 7, because dividing two integers in Rust gives an integer and drops the fractional part. Adding the two gives 16.
Step by step
- Keep only the last two digits. Take
n % 100and store it inlast_two_digits. Everything above the tens place disappears. - Extract the last digit. Take
last_two_digits % 10and store it inlast_digit. - Extract the second-to-last digit. Take
last_two_digits / 10, which is the tens digit ofn, and store it insecond_last_digit. - Add the two digits and return the sum.
The solution in Rust
impl Solution {
fn last_two_digit_sum(n: i32) -> i32 {
let last_two_digits = n % 100;
let last_digit = last_two_digits % 10;
let second_last_digit = last_two_digits / 10;
return last_digit + second_last_digit;
}
}
A few Rust details in this short function:
- Types are inferred. The parameter
nis ani32, so100and10are treated asi32too, and so are all threeletvariables. You never have to write their types. - Integer division truncates. With two integers,
/keeps only the whole part:79 / 10is7, not7.5. Rust never converts an integer division to a decimal on its own, and it refuses to mixi32andf64in one expression. letvariables can't be reassigned. Each of the three is computed once and never changes, so nomutis needed.returnwith a semicolon is valid. The usual Rust style drops the keyword and the semicolon and ends the function with the bare expressionlast_digit + second_last_digit, which gives the same result.
Complexity
The function does a fixed number of arithmetic operations whatever the size of n, so it runs in O(1) time and uses O(1) extra space.
Edge cases and common mistakes
- Zeros in the last two digits. For
n = 100, the remainder by 100 is0, so both digits are 0 and the sum is 0. Forn = 1005,1005 % 100is5, the digits are 0 and 5, and the sum is 5. The tens digit comes out as5 / 10 = 0, which is correct. - Exactly two digits. For
n = 99,99 % 100is99and the answer is 18. The remainder step changes nothing, and that is fine. - The largest
i32. For2147483647the last two digits are 4 and 7, and the sum is 11. The remainder is always below 100, so the addition can never overflow. - Dividing by 10 on the whole number.
n / 10drops the last digit but leaves all the others. To get the tens digit, dividelast_two_digits, notn. - Negative numbers. The lesson only gives non-negative inputs. In Rust,
%takes the sign of the dividend, so-2379 % 100is-79, and-79 / 10is-7because integer division truncates toward zero. The function would then return a negative sum, which is why the non-negative condition matters.
Testing
Try these inputs in the editor above: 2379 should give 16, 100 should give 0, 1005 should give 5, and 99 should give 18.
