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.
 A: Firstly, write all the integers from $0$ to $99999$ with exactly five digits, using leading zeroes if necessary. Now consider all the integers whose digits are distinct when written this way: there are $10\cdot 9\cdot 8\cdot 7\cdot 6=30240$ of these. And their average is $99999/2$ (you can see this by pairing off each such number $n$ with its 'complement' $99999-n$). So their sum is $\frac12(30240\cdot 99999)=1,511,984,880$.
This is close, but we are missing all those numbers which have one leading zero and one non-leading zero, and those which have two or more leading zeroes and at most one non-leading zero. We can categorise these by one of the following patterns ($x$ denotes a non-zero digit):
$x0xx,xx0x,xxx0,xxx,x0x,xx0,xx,x0,x$
For each of these patterns, we can find the sum of all matching integers using the above method:
$x0xx$: $9\cdot 8\cdot 7$ such numbers, with average $5055$
$xx0x$: $9\cdot 8\cdot 7$ such numbers, with average $5505$
$xxx0$: $9\cdot 8\cdot 7$ such numbers, with average $5550$
$xxx$: $9\cdot 8\cdot 7$ such numbers, with average $555$
$x0x$: $9\cdot 8$ such numbers, with average $505$
$xx0$: $9\cdot 8$ such numbers, with average $550$
$xx$: $9\cdot 8$ such numbers, with average $55$
$x0$: $9$ such numbers, with average $50$
$x$: $9$ such numbers, with average $5$
I will leave the arithmetic to you. (I did the arithmetic myself, and checked it against a brute-force count using a computer, so I am confident that this method works correctly.)
A: 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.
A: Allow $0$ as a lead digit, and regard $025$ as different from $25$.  Then the average of each digit is $4.5$.  Finally, remove numbers whose first digit is $0$, and whose other digits have average $5$.
So there are $10$ one-digit numbers, average $4.5$, $90$ two-digit numbers, average $4.5 ×11$, $720$ three-digit and so on.
Then subtract $9$ two-digit numbers that start with $0$ with average $5$, $72$ three-digit numbers starting with $0$ and average $55$, and so on.
