Sum of number's digits

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

• Welcome to stackexchange. What use will you make of this formula? If you need the result in a computer program there is a recursive algorithm that does the job neatly. You are essentially trying to do the job in a loop, which is harder. – Ethan Bolker Jul 4 '19 at 11:45
• @EthanBolker If you are using a computer program then you would just add the digits in the string representation of the number. – Peter Foreman Jul 4 '19 at 11:55
• I think the trouble is what you mean by a "math function". An algorithm describes such a function. – Couchy Jul 4 '19 at 11:56
• I'm not searching an algorithm, I want to find a math formula that I will use to solve some problems, related with digits sum... – Matteo Jul 4 '19 at 11:58
• What I mean is that you need to specify what you mean by a "math formula". For example, ask yourself why not just write $\sum_{i=1}^N d_i$? – Couchy Jul 4 '19 at 12:03

I worked on similar idea in my question many years ago. I asked to find any digit in a given number. There is an answer for my question to find any digit of a number via Fourier series expansion. Please check @Zander 's answer how to find the function. We can use it to find the sum of digit's numbers eaisly as shown below.

Let's define $$d_n(A)$$ is digit number of A, Where A is a real number. For example:

$$d_3(2345.67)=2$$

$$d_0(2345.67)=5$$

$$d_{-1}(2345.67)=6$$

We can write easily $$d_n(A) = d_0(10^{-n}A)$$ and $$A = \sum_{k=-\infty}^\infty 10^k d_k(A)$$ $$A = \sum_{k=-\infty}^\infty 10^k d_0(10^{-k}A)$$

If you notice the formulas above , it is enough to find out only $$d_0(A)$$.

If we find $$d_0(A)$$ ,any digit can be found by using it. $$d_0(A) =\frac{9}{2} -\frac{10}{\pi}\sum_{k=1}^\infty b_k \sin\left(\frac{k\pi A}{5}\right)$$ where $$b_k = \begin{cases}0 & \mathrm{if}~10\mid k\\ 1/k & \mathrm{otherwise}\end{cases}$$

Thus we can write the sum of integer number's digits :$$T$$

If A is a positive integer , you can write $$T$$ as $$T=\sum_{k=0}^\infty d_0(10^{-k}A)$$

• Thanks a lot @Mathlover. Is there a method to evaluate this infinite sum (maybe with integrals)? – Matteo Jul 4 '19 at 14:52

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)

I know this is a strange idea, and not an answer to the question, but it's possible that you can do something useful by reversing the task.

That is to say, instead of starting with $$n$$, you can say "I've got a digit sequence $$d_k, d_{k-1}, \ldots, d_0$$, with $$d_k \ne 0$$, and $$0 \le d_i \le 9$$ for all $$i$$, and this represents the number $$n$$ in base $$10$$," so that $$n = \sum_{i = 0}^k d_k 10^k.$$

Now you can do things like talk about $$\log_{10} n$$ (which is between $$k$$ and $$k+1$$, and is approximately $$k + \log d_k$$, for instance), and other functions of $$n$$ may similarly be expressible in terms of the $$d_i$$.

Of course, without knowing details --- "I want to find a math formula that I will use to solve some problems, related with digits sum..." is a little bit vague --- it's tough to know whether this approach could be of any use.

Post-comment addition Here's a little matlab program that solves your problem (for not-too-large cases) using the approach I described:

function s = digitpower(k, a)
% Find numbers containing k or fewer digits with the property that the
% number equals v^a, where v is the sum of the digits of the number.

if (k == 0)
s = [];
end

trials = digits(k); % the first 10^k numbers, as digit sequences.
powervec = 10.^((k-1):-1:0);
nums = powervec * trials;
sums = sum(trials, 1);
pows = sums .^ a;
s = find (nums == pows);
s = nums(s);


To make that run, you also need the "digits" program:

function s = digits(k)
% produce all digit-sequences of length k, in a k x 10^k matrix,
% where each column represents a digit-sequence.
if k < 1
error( "Can't get digit sequences of 0-digit numbers");
end

if k == 1
s = 0:9;
return;
end

q = digits(k-1);
r = size(q, 1);
c = size(q, 2);
qp = repmat(q, 1, 10);
h = repmat((0:9)', 1, c)';
h = reshape(h, 1, []);
s = [h;qp];


Some sample executions of the program look like this:

>> digitpower(2, 1)

ans =     0     1     2     3     4     5     6     7     8     9

>> digitpower(2, 2)

ans =
0     1    81

>> digitpower(2, 3)

ans =     0     1

>> digitpower(3, 3)

ans =     0     1   512

>> digitpower(4, 3)

ans =           0           1         512        4913        5832


THe first one says that the only numbers with 0, 1, or 2 digits whose digit-sum, to the first power, is equal to the number itself, are the numbers 0 through 9.

The second says that among 0-2-digit numbers, those whose digit-sum, squared, equal the number are 0, 1, and 81.

The last says that among numbers of no more than 4 digits, those that equal the cube of their digit-sum are 0, 1, 512, 4913, and 5832.

Those last ones surprised me a bit -- I didn't really expect to see as many positive results as this.

• Thanks for your answer, I will correct. – Matteo Jul 4 '19 at 13:17
• I would like to find some number that can be written as a power of the sum of their digits. – Matteo Jul 4 '19 at 14:53
• So you'd like to find a digit-sequence with the property that $n = \sum_{i = 0}^k d_k 10^k= \left( \sum_{i = 0}^k d_k \right)^a$, for some (integer?) $a$. That seems like a reasonable question, and one that's reasonable to express in terms of the digits $d_i$ instead of the number $n$. I wrote a short program to verify this, and search for such numbers, and found a few. See the "post-comment additions" in my answer. – John Hughes Jul 4 '19 at 15:46
• Yes, the exponent must be an integer; but if we search for example the 50th term it would be very difficult only by bruteforcing. At the contrary if we find a non recursive function, we obtain an exponential equation, so it would be simple to map the solutions... At first I also have tried your method, but it was too long... – Matteo Jul 4 '19 at 16:02
• As I said, this was merely a demonstration that search for digit-sequences provides a workable approach without the need for the "extract digit" function. If you want to solve this problem for large values of $k$, I wish you the best of luck, but have no further insights. Well..maybe one: I suspect that the "extract digit" function will involve computation (either directly, or in one of the component functions) on the order of at least $\log n$, so that computing the digit-sum power will involve $n \log n$ time, making it impractical for numbers like $10^{50}$. But I could well be wrong. – John Hughes Jul 4 '19 at 17:01

If you allow the greatest integer function (whose domain is the set of reals and whose range is the set of integers) and the "mod $$10$$" function (whose domain is the set of integers and whose range is the set of residues from $$0$$ to $$9$$), then, for positive integers $$A$$, we have

$$S(A)=\sum_{n=0}^\infty\left(\left\lfloor A\over10^n\right\rfloor\mod10 \right)$$

or, written somewhat more abstractly, with obvious interprets of the symbols,

$$S(A)=\sum_{n=0}^\infty m_{10}(g(A/10^n))$$

E.g.,

\begin{align} S(1234) &=(\lfloor1234\rfloor\mod10)+(\lfloor123.4\rfloor\mod10)+(\lfloor12.34\rfloor\mod10)+(\lfloor1.234\rfloor\mod10)+\cdots\\ &=(1234\mod10)+(123\mod10)+(12\mod10)+(1\mod10)+(0\mod10)+\cdots\\ &=4+3+2+1+0+0+0+0+\cdots\\ &=10 \end{align}

Note, the upper limit $$\infty$$ in the sum can be replaced with something finite but depending on $$A$$. The "obvious" replacement is $$\lfloor\log_{10}A\rfloor$$, but the "easiest" is simply $$A$$ itself.

In a (deleted) answer/comment, the OP remarked they were interested in finding a number that is a power of the sum of its digits, i.e., $$A=S(A)^k$$ for some $$k\ge1$$. The first nontrivial example (with $$A\ge10$$) is $$A=81$$, and after that come $$A=512$$ and $$A=2401$$. The sequence can be found at https://oeis.org/A023106 .