Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum individual digits of integer?

Tags:

arrays

c#

I have an integer value (ex: 723) and i want to add up all the values in this integer until i get a single value.

ex: 7 + 2 + 3 = 12
    1 + 2     = 3

I'm new to C#. please give me a good explanation of your answer as well :)

like image 255
megazoid Avatar asked Mar 01 '11 06:03

megazoid


People also ask

What is the sum of the digits of the integer?

What is digit sum? We can obtain the sum of digits by adding the digits of a number by ignoring the place values. So, for example, if we have the number 567 , we can calculate the digit sum as 5 + 6 + 7 , which will give us 18 .

How do you find the sum of numbers from 1 to 50?

And hence the sum of the first 50 natural numbers to be 1275.

What is the sum of all of the digits of the integers from 1 to 100?

5050.


2 Answers

Though the solutions where you pull out the bottom digit and divide by ten are correct and clearly implement the desired function, you can do this task in far less code if you know a trick. If you sum the digits as you describe until you get a single-digit number, the result you get is the remainder when you divide the original number by nine.

Try it. 789 --> 7 + 8 + 9 = 24 --> 2 + 4 --> 6, and 789 = 87 * 9 + 6

So you can solve your problem by just doing x % 9 if x is a positive integer. If you get zero, then the real result is nine, otherwise you get the repeated sum of the digits.

This trick leads to a way of checking arithmetic called "casting out nines". Suppose you have a sum and you want to check if it is correct:

  3147 
+ 5926
  ----
  9063  

Is that correct? Do your trick on the each line:

  3147 --> 3 + 1 + 4 + 7 = 15 --> 1 + 5 = 6
+ 5926 --> 5 + 9 + 2 + 6 = 22 --> 2 + 2 = 4
  ----
  9063 --> 9 + 0 + 6 + 3 = 18 --> 1 + 8 = 9

Now do the trick on the sum. 6 + 4 = 10 --> 1 + 0 = 1 If you did the original math right then the two checksums should be equal, but they are not, the first is 1 and the second is 9. And sure enough, there is an error in the tens place. The correct sum is

  3147 --> 3 + 1 + 4 + 7 = 15 --> 1 + 5 = 6
+ 5926 --> 5 + 9 + 2 + 6 = 22 --> 2 + 2 = 4
  ----
  9073 --> 9 + 0 + 7 + 3 = 19 --> 1 + 9 = 10 --> 1 + 0 = 1

And now the checksums are the same. 6 + 4 = 10 --> 1 + 0 = 1

It's called "casting out nines" because you can ignore any nines that are in the sum, because they don't make any difference:

9123 --> 9 + 1 + 2 + 3 = 15 --> 1 + 5 = 6, which is the same as just 1 + 2 + 3. You can "cast out" the nine and still get the same result.

Now, can you prove that the sum of the digits is the remainder when dividing by nine? Can you prove that casting out nines works for sums? Can you deduce and prove a similar rule for checking products for errors?

SPOILERS BELOW

Let's define a relation x≡c which means "x and c are non-negative integers and there exists a non-negative integer n such that x = 9n + c". That is, x and c are "congruent mod nine". Got it?

First thing to prove: if x≡c and y≡d then x+y≡c+d.

That's straightforward. By definition of the relation there exist non-negative integers m and n such that x = 9n + c and y = 9m + d. We must show that there exists a non-negative integer p such that x + y = 9p + c + d. That integer p is obviously m + n. Since there exists such an integer, the relation holds.

Second thing to prove: if x≡c and y≡d then xy≡cd.

Again, we must show that there exists an integer p such that xy = 9p + cd. By similar proof of the first theorem, p = 9nm + mc + nd works, so the relation holds.

Third thing to prove: 10n≡1 for any non-negative integer n.

The proof is easy by induction:

  • Clearly 100≡1
  • Clearly 101≡1
  • Make the inductive hypothesis: suppose 10k≡1 where k > 0.
  • 10k+1=10110k
  • 10110k≡(1)(1) by our second proof.
  • Therefore if 10k≡1 then 10k+1≡1
  • Therefore by induction 10n≡1 for all non-negative integers n.

From these three theorems you can now see that

a(102) + b(101) + c(100) ≡ a + b + c

So we have shown that a number in decimal notation is "congruent mod nine" to the sum of its digits.

The fact that "casting out nines" works as an arithmetic checksum now follows immediately from our first proof.

like image 185
Eric Lippert Avatar answered Sep 23 '22 17:09

Eric Lippert


int i = 723;
int acc;
do {
    acc = 0;
    while (i > 0)
    {
        acc += i % 10;
        i /= 10;
    }
    i = acc;
} while(acc>=10);

% 10 gives the final digit each time, so we add that into an accumulator. /= 10 performs integer division, essentially removing the final digit each time. Then we repeat until we have a small enough number.

like image 29
Marc Gravell Avatar answered Sep 20 '22 17:09

Marc Gravell