<s>[INST] What is the sum of natural numbers that have $5$ digits or less, and all of the digits are distinct? $1+2+3+\dots+7+8+9+10+12+13+\dots+96+97+98+102+103+104+\dots+985+986+987+1023+1024+1025+\dots+9874+9875+9876+10234+10235+10236+\dots+98763+98764+98765=$
The only thing I can do is to evaluate a (bad) upper bound by evaluating the sum of natural numbers from $1$ to $98765$, that is equal to $98765 \times (98765+1)/2=4,877,311,995$. So the desired answer is less than that.
Any help would be appreciated. Thanks.
 [/INST] This is the approach of Empy2 (which I believe didn’t deserve any downvotes) spelled out.
Consider the numbers for each length separately.
First allow a leading zero. Then there are $\frac{10!}{(10-k)!}$ different numbers with $k$ digits, all distinct, and they sum to $10^k-1$ in pairs, so their sum is $\frac{10^k-1}2\cdot\frac{10!}{(10-k)!}$.
Now subtract the numbers with a leading zero. There are $\frac{9!}{(10-k)!}$ of these, their average digit is $5$, and the total value of their digits is $\frac{10^{k-1}-1}9$, so they sum to $5\left(10^{k-1}-1\right)\cdot\frac{8!}{(10-k)!}$.
Thus, the overall sum is
\begin{eqnarray}
&&\sum_{k=1}^5\left(
\frac{10^k-1}2\cdot\frac{10!}{(10-k)!}-5\left(10^{k-1}-1\right)\cdot\frac{8!}{(10-k)!}\right)\\
&=&
\sum_{k=0}^5\frac{10^k-1}2\cdot\frac{10!}{(10-k)!}-\sum_{k=0}^45\left(10^k-1\right)\frac{8!}{(9-k)!}
\\
&=&
\frac{10^5-1}2\frac{10!}{5!}+\sum_{k=0}^4\frac{8!\left(10^k-1\right)}{(10-k)!}\left(45-5(10-k)\right)
\\
&=&
\frac{10^5-1}2\frac{10!}{5!}+\sum_{k=0}^4\frac{8!\left(10^k-1\right)}{(10-k)!}(5k-5)
\\
&=&
1511984880+8479575
\\
&=&
1520464455\;.
\end{eqnarray}
The result is confirmed by this Java code.
</s>