Here is the thing. I solved kata on Codewars with task: Find number of carries.
I solved this task trivially. I reversed both numbers and sum each digit one by one. I accumulate sum / 10 (integer division). I increase counter each time when accumulator isn't equal 0. My solution is 41 lines-long. I submitted my solution and I founded another solution and I don't understand how to works! I tried it on paper all works!
Please explain it to me.
The smart solution
- sum_a = sum of digits for first number
- sum_b = sum of digits for second number
- sum_sum = sum of digits for sum of numbers
- (sum_a + sum_b - sum_sum) / 9(integer division)
examples:
### first
sum(543) = 12
sum(3456) = 18
543 + 3456 = 3999
sum(3999) = 30
(12 + 18 - 30) => 0/9 = 0 right!
second
sum(1927) = 19
sum(6426) = 18
1927 + 6426 = 8153
sum(8153) = 17
(19 + 18 - 17) => 20/9 = 2 right!