I'm a CS student and I bought Cracking the Coding Interview about a week ago. I'm just at the Big O chapter and I found an algorithm which supposedly sums the digits in a number; it looked confusing at a first glance so I ran it in Python but it did not do what it was supposed to. Have a look:
int sumDigits(int n) {
int sum= 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
From what I understand, this code does not actually return the sum of the digits of a given number, or does it? I really want to see how it does this but since the example I tried in Python did not work I really can't see how this works.