Formula for calculating the difference between sums of digits in the same base for $(n-1)$ and $n$

80 Views Asked by At

I am looking for a formula to calculate the difference between the sums of the digits of $(n-1)$ and $n$ in a given base, denoted as $sb(n-1) - sb(n)$. Specifically, I want to find a formula that works for any positive integer $n$ and base $b$.

Is there a general formula that can directly calculate this difference? I would appreciate any insights or explanations regarding the derivation of such a formula.

Thank you!

1

There are 1 best solutions below

0
On BEST ANSWER

The basic idea is to see how the digits change when $1$ is added.

If the units digit is not $b-1$, there are no carries, so the sum of the digits increases by $1$,

If the last $k$ digits are $b-1$ and the digits before is not $b-1$, then the last $k$ digits change from $b-1$ to $0$ and the preceding digit increases by $1$ so the digit sum is reduced by $k(b-1)-1$. Note that if $k=0$, this reduces to the case above.

So, if $kb(n)$ is the number of trailing $b-1$'s, then $sb(n) =sb(n-1)-(b-1)kb(n-1)+1 $.

The problem is thus reduced to computing $kb(n)$.

I have some ideas on this, but the details are proving annoying to figure out, so I'll leave it at this for now. If I do figure them out, I will add them to this answer.