[Math] Sum of number’s digits

modular arithmeticmodular-functionnumber theorysummation

I'm looking for a math function that taking an integer returns the sum of it's digits. I tried with this method:

-I've got a digit sequence $d_{k+1},d_{k},…,d_{1}$ with $d_{k+1}\ne 0$ and $d_i \in \{0,1,2 \dots , 9\}$ for all $1 \le i \le k+1$; this represents the number $A$ in base $10$, so: $$A = \sum_{i = 1}^{k+1} d_i 10^{i-1}$$

-Now I know that each digit is generated by: $$d_i = \sum_{i=1}^{\left \lfloor \log_{10}(A)+1 \right \rfloor}\frac{A\,\, \pmod{10^i}-A\,\, \pmod{10^{i-1}}
}{10^{i-1}}$$

-I expand the sum and I obtain: $$\frac{A\, \pmod{10}-A\,\, \pmod1}{1}+\frac{A\, \pmod{10^2}-A\,\, \pmod{10}}{10}+\cdots +\frac{A\, \pmod{ 10^i}-A\pmod{10^{i-1}}}{10^{i-1}}$$

-I have to simplify the denominator so: $$\frac{1\cdot (A\, \pmod{10}-A\,\, \pmod1)}{1}+\frac{10\cdot (\lfloor\frac{A}{10}\rfloor\, \pmod{10}-\lfloor\frac{A}{10}\rfloor\,\, \pmod 1)}{10}+\cdots +\frac{10^{i-1}\cdot (\lfloor{\frac{A}{10^{i-1}}}\rfloor\, \pmod{10}-\lfloor\frac{A}{10^{i-1}}\rfloor\,\, \pmod 1)}{10^{i-1}}$$

-I split the term: $$A\, \, \pmod{10}+\lfloor\frac{A}{10}\rfloor\, \, \pmod{10}+\cdots+\lfloor\frac{A}{10^{i-1}}\rfloor\, \, \pmod{10}-(A\, \, \pmod1+\lfloor\frac{A}{10}\rfloor\, \, \pmod1+\cdots +\lfloor\frac{A}{10^{i-1}}\rfloor\, \, \pmod1)$$

-Now I would have picked up $\pmod{10}$ and $\pmod1$: $$\left (\sum_{i=1}^{\left \lfloor \log_{10}(A)+1 \right \rfloor}\lfloor\frac{A}{10^{i-1}}\rfloor\right)\, \, \pmod{10}-\left (\sum_{i=1}^{\left \lfloor \log_{10}(A)+1 \right \rfloor}\lfloor\frac{A}{10^{i-1}}\rfloor\right)\, \, \pmod1$$; but I can't. Any ideas to move forward?

Thanks

Best Answer

Here's a "formula" for the sum of the digits of $n$:

$$s(n) = \sum_{k=0}^{\lfloor\log_{10} n\rfloor}\left(\left\lfloor\frac{n}{10^k}\right\rfloor-10\left\lfloor\frac{n}{10^{k+1}}\right\rfloor\right)$$

Note that each summand gives you a digit.

This can be simplified to $$s(n)= n - 9 \sum_{k=1}^{\lfloor\log_{10} n\rfloor}\left\lfloor\frac{n}{10^k}\right\rfloor$$

Note however that the term "formula" is not precisely defined. It's a term that depends of the context. It can mean very different things depending of the field you're working.

I think that in number theory or combinatorics one would allow the use of the floor and log functions, so one would consider this a formula (although not a "closed" formula, because the number of operations is not bounded)