How to sum individual digits of integer?
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 :)
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.
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.
Use Mod (%) recursively:
int AddUp(int number){
if(number<10) return number;
return AddUp(number/10) + number % 10;
}
hmm, a fast function come to my mind is
int number = 723;
double sum = number.ToString().Sum(s => Char.GetNumericValue(s));
string numString = number.ToString();
and for calculating all sums with LINQ use
double [] allSums = Enumerable.Range(1, numString.Length).Select(i =>numString.Substring(0, i).Sum(s => Char.GetNumericValue(s))).ToArray();
int a=723;
if(a % 9 ==0){
result = 9;
}
else if(a % 9 !=0){
result = a % 9;
}
精彩评论