Is there a way to count how many carries are required for an addition problem without actually performing the addition?

43 Views Asked by At

$501 + 89$ requires one carry in base $10$. Naively I might just match up the positions and count how many of them add to at least $10$. But sometimes this doesn't work. For example $499 + 1$ requires $2$ carries even though only one digit pair adds to more than $10$. I can account for this by adding one to the tens place and check whether that sums to more than $10$, but at that point I'm basically computing the addition in full. Is this the fastest way to tell how many carries are required to compute the addition in base $10$?

1

There are 1 best solutions below

0
On BEST ANSWER

Unfortunately, you need the sum.

But if you already have $a, b$ and $s = a+b$, then you can determine the number of carries.

You can compute $d = a \oplus b$ (i.e., the digit sum modulo $10$) and then compute $s \ominus d$ (i.e., the digit difference modulo $10$). The number of non-zero digits in $s \ominus d$ is the number of carries.

For e.g.,: $501 + 89 = 590 = s$. We have $d = 501 \oplus 089 = 580$ and $s \ominus d = 590 - 580 = 010$. Since there is $1$ non-zero digit in $s \ominus d$, the number of carries required is $1$.

Another e.g.,: $499 + 1 = 500 = s$. We have $d = 499 \oplus 001 = 490$ and $s \ominus d = 500 - 490 = 110$. Since there are $2$ non-zero digits in $s \ominus d$, the number of carries required is $2$.