This is the problem : repeatedly add all its digits until the result has only one digit.
given the arbitrary number : 96491
This is the first solution that is pretty straight forward and I can understand. The result is $2$.
$(9+6+4+9+1 = 29) => (2+9 = 11) => (1+1 = 2) => 2$
Now here is another solution that I don't understand why it works.
$(9649 + 1 = 9650) => (965 + 0 => 965) => (96 + 5 = 101) => (10+1 = 11) => (1 +1 = 2) => 2$
What just happened? as you see the steps and results during steps seems completely different. yet they always produce same result. this number was just example. it works for any given positive number (tested with program for millions of different numbers). I'm really curios why second solution works? Thanks for explanations.
Answer
Arthur already pointed out the main reason, but I want to clarify it a bit :
The remainder modulo $9$ does not change if we replace any number by the sum of its digits, this can be easily seen : If we have $$N=a_k\cdot 10^k+a_{k-1}\cdot 10^{k-1}+\cdots+a_1\cdot 10+a_0$$ then we have $$N\equiv a_k+a_{k-1}+\cdots +a_1+a_0\mod 9$$ because of $$10^n\equiv 1\mod 9$$ for every non-negative integer $n$.
The final result is $N\mod 9$ , if $N$ is not divisible by $9$ and $9$ , if $N$ is divisble by $9$. As Arthur pointed out, the formula $$((N-1)\mod 9)+1$$ covers all cases.
No comments:
Post a Comment