Saturday, November 14, 2020

A one-line formula to add the digits of a 2-digit number using mod

Adding two digits of a number is the same as subtracting 9 multiplied by the number in the tens from the original number. This formula uses mod(%) to isolate such number and provides the result of the addition of the 2 digits of a given number from 11 to 99:

From: x- (((x -(x mod 10)) / 10) *9)
To: f(x) = x - (1/10(x - x mod 10))× 9
Example: 23 -> 2+3 = 5 | 23 - (9*2) = 5 | 23 - (((23 - (23 mod 10)) / 10) * 9) = 5 
Sample in Ctwodigits




No comments:

Post a Comment