I recently had a test in my class. One of the problems was the following:
Given a number n, write a function in C/C++ that returns the su
The problem statement is confusing, but the numerical example clarifies the meaning of the sum of the digits of the number squared. Here is an improved version:
Write a function in the common subset of C and C++ that takes an integer
n
in the range [-107, 107] and returns the sum of the squares of the digits of its representation in base 10. Example: ifn
is123
, your function should return14
(12 + 22 + 32 = 14).
The function that you wrote is fine except for 2 details:
long
to accommodate for all values in the specified range as type long
is guaranteed by the C Standard to have at least 31 value bits, hence a range sufficient to represent all values in [-107, 107]. (Note that type int
is sufficient for the return type, whose maximum value is 568
.)%
for negative operands is non-intuitive and its specification varied between the C99 Standard and previous editions. You should document why your approach is valid even for negative inputs.Here is a modified version:
int sum_of_digits_squared(long n) {
int s = 0;
while (n != 0) {
/* Since integer division is defined to truncate toward 0 in C99 and C++98 and later,
the remainder of this division is positive for positive `n`
and negative for negative `n`, and its absolute value is the last digit
of the representation of `n` in base 10.
Squaring this value yields the expected result for both positive and negative `c`.
dividing `n` by 10 effectively drops the last digit in both cases.
The loop will not be entered for `n == 0`, producing the correct result `s = 0`.
*/
int c = n % 10;
s += c * c;
n /= 10;
}
return s;
}
The teacher's answer has multiple flaws:
int
may have an insufficient range of values.0
.n = INT_MIN
.Given the extra constraints in the problem statement (C99 and range of values for n
), only the first flaw is an issue. The extra code still produces the correct answers.
You should get a good mark in this test, but the explanation is required in a written test to show your understanding of the issues for negative n
, otherwise the teacher may assume that you were unaware and just got lucky. In an oral exam, you would have gotten a question and your answer would have nailed it.