7

The digit sum of a number is the sum of the digits in the number. Let $n$ be a natural number.

Then the digit sum of $n$ is $S(n)=\displaystyle\sum_{k=0}^{\lfloor{\log_{10} n}\rfloor}\frac{1}{10^k}\left(n\mod10^{k+1}-n\mod10^k\right)$, as is seen in this wiki page (I am interested in the base $10$ case only).

I am aware that the number of digits in $n$ equals $\lfloor{\log_{10} n}\rfloor+1$ in base $10$. Numerous posts are there regarding how to write a computer code for finding the sum of digits in a given positive number. However, I haven't found any source of the digit sum formula and I want to know how it is derived. I just need the basic idea. Any link to a source would be great too.

StubbornAtom
  • 18,135

4 Answers4

8

In this formula, $n\mod10^k$ means the remainder after $n$ is divided by $10^k$. Notice that this is precisely the last $k$ digits of $n$, e.g. $4355\mod 100=55$. Therefore $(n\mod 10^{k+1} - n\mod 10^k)$ is $10^k$ times the $k$-th digit of $n$.

Jack M
  • 28,748
5

The following example easily makes clear the logic of the formula. Let's take: $$N=123456$$ Then the third digit is: $$\frac{(3456-456)}{1000}=\frac{(N\mod 10000)-(N\mod 1000)}{1000}$$

I think just summing up the digits is efficient enough to determine the digit-sum. In PARI/GP, the number is converted to a vector and the components are just summed up. No clue whether there is a better method in general.

q-l-p
  • 189
Peter
  • 87,158
2

$N = \sum_{i=0}^n a_i*10^i$.

What is the $k$th digit[1] in terms of $N$?

$N \mod 10^{k+1} = \sum_{i=0}^k a_i*10^i$. That is "stripping off" all the digits past $k$ and keeping just the first $k$.

$N\mod 10^{k} = \sum_{i=0}^{k-1} a_i*10^i$. That is "stripping off" all the digits at $k$ and beyond, and keeping just the first $k-1$ digits.

$N \mod 10^{k+1}-N\mod 10^{k} = \sum_{i=0}^k a_i*10^i -\sum_{i=0}^{k-1} a_i*10^i = a_k*10^k$. We stripped off all the digits past the $k$th digit. And then we subtracted all the ones before the $k$th digit. So all that is left is the $k$th digit (times the appropriate power of $10$).

Hence $\frac 1{10^k}(N \mod 10^{k+1}-N\mod 10^{k}) = a_k$; the $k$th digit.

So $\sum_{k=0}^n a_k = \sum_{k=0}^n \frac 1{10^k}(N \mod 10^{k+1}-N\mod 10^{k})$.

And... as you know, if $N$ has $n+1$ digits then $n = \lfloor \log N\rfloor$.

So $\sum_{k=0}^n a_k = \sum_{k=0}^{\lfloor \log N\rfloor} \frac 1{10^k}(N \mod 10^{k+1}-N\mod 10^{k})$

[1] I'm taking "$k$th digit" to mean digit in the $10^k$ position. I suppose one counld nitpick and say that is really the $k + 1$th digit, (or the $n+1 - k$th digit) but... let's not.

fleablood
  • 134,278
1

Late to the party, but as far as I can tell, this formula works too:

$$ S\!\left(x\right) = x - 9\sum_{i=1}^N \left\lfloor\tfrac{x}{{10}^i}\right\rfloor, \text{ where } N = \left\lfloor\log_{10}\!\left(x\right)\right\rfloor. $$

For instance, if we take $x = 7081$, we have $708 + 70 + 7 = 785$, and indeed $7081 - 9 \times 785 = 16$ is the right digit sum (i.e., $7 + 0 + 8 + 1 = 16$).

I think its implementation is simpler and more efficient than the "naive" approach.

Naive approach:

def calc_digit_sum(x: int) -> int:
    s = 0

while x > 0:
    s += x % 10
    x //= 10

return s

This formula's:

def calc_digit_sum_formula(x: int) -> int:
    x_ = x
    s = 0

while x_ > 0:
    x_ //= 10
    s += x_

return x - 9 * s

I'm guessing it'd be pretty simple to adapt this formula to other numerical bases (i.e., by substituting all 10 by the new basis value, keeping in mind that $9 = 10 - 1$), but I haven't had the chance to test it out yet.

Also, I don't have a proof on hand for the formula as we speak. I just printed the digit sums for all integers from 0 to 999, happened to "see" the formula, ran a quick (and conclusive) simulation test, and that was it. Probably not that hard to demonstrate, though.

Oh, nevermind, I have the proof here:

$$ \begin{array}{rcl} S\!\left(x\right) & = & \sum_{i=0}^N \tfrac{x \mod {10}^{i + 1} - x \mod {10}^i}{{10}^i}\\ & = & \sum_{i=0}^N \tfrac{x - {10}^{i + 1}\left\lfloor\tfrac{x}{{10}^{i + 1}}\right\rfloor - \left(x - {10}^i\left\lfloor\tfrac{x}{{10}^i}\right\rfloor\right)}{{10}^i}\\ & = & \sum_{i=0}^N \left\lfloor\tfrac{x}{{10}^i}\right\rfloor - 10\sum_{i=0}^N \left\lfloor\tfrac{x}{{10}^{i + 1}}\right\rfloor\\ & = & x + \sum_{i=1}^N\left\lfloor\tfrac{x}{{10}^i}\right\rfloor - 10\left(\sum_{i=1}^N\left\lfloor\tfrac{x}{{10}^i}\right\rfloor + \left\lfloor\tfrac{x}{{10}^{N + 1}}\right\rfloor\right)\\ & = & x - 9\sum_{i=1}^N\left\lfloor\tfrac{x}{{10}^i}\right\rfloor.\\ \end{array} $$

We see that for $10 \leq x < 20$, this formula simplifies to $S\!\left(x\right) = x - 9$, which is consistent with Luhn's checksum algorithm, so that's nice.

Here's another proof, in case the first one wasn't enough... since we have $x = \sum_{j=0}^N a_j {10}^j$, it follows that:

$$ \begin{array}{rcl} S\!\left(x\right) & = & x - 9\sum_{i=1}^N\left\lfloor\tfrac{x}{{10}^i}\right\rfloor\\ & = & x - 9\sum_{i=1}^N\sum_{j=i}^N a_j {10}^{j - i}\\ & = & x - 9\sum_{j=1}^N\sum_{i=1}^j a_j {10}^{j - i}\\ & = & x - 9\sum_{j=1}^N a_j {10}^j \sum_{i=0}^{j-1} {10}^{-\left(i + 1\right)}\\ & = & x - 9\sum_{j=1}^N a_j {10}^{j - 1} \tfrac{1 - {\left(1/10\right)}^{j - 1 + 1}}{1 - 1/10}\\ & = & x - \left(\sum_{j=1}^N a_j {10}^j - \sum_{j=1}^N a_j\right)\\ & = & x - \left(x - a_0 - \sum_{j=0}^N a_j + a_0\right)\\ & = & \sum_{j=0}^N a_j.\\ \end{array} $$

Should be obvious from there that it also applies to any numerical basis.

Now, can we use this formula to extract individual digits? Sure! First, let's just express the $j$-th digit of $x$ as:

$$ a_j = S\!\left(\left\lfloor\tfrac{x}{{10}^j}\right\rfloor\right) - S\!\left(\left\lfloor\tfrac{x}{{10}^{j + 1}}\right\rfloor\right). $$

We have

$$ \begin{array}{rcl} S\!\left(\left\lfloor\tfrac{x}{{10}^j}\right\rfloor\right) & = & \left\lfloor\tfrac{x}{{10}^j}\right\rfloor - 9\sum_{i=1}^N \left\lfloor\left\lfloor\tfrac{x}{{10}^j}\right\rfloor / {10}^i\right\rfloor\\ & = & \left\lfloor\tfrac{x}{{10}^j}\right\rfloor - 9\sum_{i=1}^N \left\lfloor\tfrac{x}{{10}^{i+j}}\right\rfloor\\ & = & \left\lfloor\tfrac{x}{{10}^j}\right\rfloor - 9\sum_{i=j+1}^{N+j} \left\lfloor\tfrac{x}{{10}^i}\right\rfloor\\ & = & \left\lfloor\tfrac{x}{{10}^j}\right\rfloor - 9\sum_{i=j+1}^N \left\lfloor\tfrac{x}{{10}^i}\right\rfloor.\\ \end{array} $$

Therefore,

$$ \begin{array}{rcl} a_j & = & \left\lfloor\tfrac{x}{{10}^j}\right\rfloor - 9\sum_{i=j+1}^N \left\lfloor\tfrac{x}{{10}^i}\right\rfloor - \left\lfloor\tfrac{x}{{10}^{j + 1}}\right\rfloor + 9\sum_{i=j+2}^N \left\lfloor\tfrac{x}{{10}^i}\right\rfloor\\ & = & \left\lfloor\tfrac{x}{{10}^j}\right\rfloor - 10\left\lfloor\tfrac{x}{{10}^{j + 1}}\right\rfloor\\ & = & \left\lfloor\tfrac{x}{{10}^j}\right\rfloor \mod 10,\\ \end{array} $$

which is poised to be the least controversial result here presented, and also ties nicely with the original formula. Indeed, for the special case $j = 0$, the result simplifies to $a_0 = x - 10\left\lfloor\tfrac{x}{10}\right\rfloor = x \mod 10$, which is self-evident.

// ETA: Added a numerical example, as well as a small note on the origin of this formula.

// ETA2: Added a mathematical proof, and comment on Luhn's Checksum algorithm.

// ETA3: Added a second mathematical proof.

// ETA4: Added a section on individual digit extraction.

// ETA5: Added minor clarification for digit extraction.

RSMax
  • 31