# Formula for reversing digits of positive integer $n$

I was able to work out the cases for $n$ having up to $4$ digits and was wondering if someone could verify my generalization to $m$ digits. Here I am assuming that when a reversal results in there being a leading $0$ it get ignored (e.g. $76130$ gets reversed to $3167$, $998700$ to $7899$ etc.)

I believe the function can be expressed as $r(n): \mathbb{N} \rightarrow \mathbb{N}$
$$r(n) = \left \lfloor \frac{n}{10^m}\right \rfloor +\sum_{k=0}^{m-1}10^{m-k} \left( \left \lfloor\frac{n}{10^k} \right \rfloor-10 \left \lfloor \frac{\left \lfloor \frac{n}{10^k}\right \rfloor}{10}\right \rfloor \right)$$

where again, $n$ has $m$ digits.

It seems like this combination of the floor function and powers of 10 is the only way to achieve this but is this true?

#### Solutions Collecting From Web of "Formula for reversing digits of positive integer $n$"

Your first term is always $0$, and you’re off by a factor of $10$; for example, by your formula

\begin{align*} r(123)&=\left\lfloor\frac{123}{100}\right\rfloor+\sum_{k=0}^210^{3-k}\left(\left\lfloor\frac{123}{10^k}\right\rfloor-10\left\lfloor\frac1{10}\left\lfloor\frac{123}{10^k}\right\rfloor\right\rfloor\right)\\\\ &=0+1000(123-120)+100(12-10)+10(1-0)\\ &=3210 \end{align*}

instead of the correct $321$. However,

$$r(n)=\sum_{k=0}^{m-1}10^{m-1-k}\left(\left\lfloor\frac{n}{10^k}\right\rfloor-10\left\lfloor\frac1{10}\left\lfloor\frac{n}{10^k}\right\rfloor\right\rfloor\right)$$

does the trick.

Suppose that $n=\sum_{i=0}^{m-1}d_k10^i$, where each $d_i\in\{0,1,\dots,9\}$, and $d_{m-1}\ne 0$, so that the decimal expansion of $n$ is $d_{m-1}d_{m-2}\ldots d_1d_0$. Then

$$\left\lfloor\frac{n}{10^k}\right\rfloor=\sum_{i=k}^{m-1}d_i10^{i-k}=10\sum_{i=k+1}^{m-1}d_i10^{i-k}+d_k\;,$$

so

$$\left\lfloor\frac{n}{10^k}\right\rfloor-10\left\lfloor\frac1{10}\left\lfloor\frac{n}{10^k}\right\rfloor\right\rfloor=\left(10\sum_{i=k+1}^{m-1}d_i10^{i-k}+d_k\right)-10\sum_{i=k+1}^{m-1}d_i10^{i-k}=d_k\;,$$

and

$$r(n)=\sum_{k=0}^{m-1}10^{m-1-k}d_k=\sum_{k=0}^{m-1}d_{(m-1)-k}10^k\;,$$

whose decimal expansion is $d_0d_1\ldots d_{m-2}d_{m-1}$, as desired.

This is what I came up with:
$$\sum_{k=1}^{n}10^{k-1}\frac{xmod10^{n-k-1}-xmod10^{n-k}}{10^{n-k}}$$
Where n is the number of digits in the number x, it could be replaced with $\lfloor{log_{b}x}\rfloor+1$
I believe it works in different bases (just change 10 for the desired base), although it has only been tested in base 10! Taking the first term k = 1, with n = 3 for example gives:
$$\frac{xmod1000-xmod100}{100}$$
The $xmod1000$ term returns the original number, the $xmod100$ term returns the remainder after division by 100 which will be the two digit number after the “number of hundreds”, the result will then be the number of hundreds eg. for 345 the numerator will be 300, dividing by 100 returns 3. The next term in the series essentially does the same thing, the fraction returns the value the next power of ten along with one important difference; the $10^{k-1}$ term makes the digit be worth ten times what it was so in the 345 example the 3 will be worth 3 instead of 300 the for will remain at the same value of 40 and the 5 will be worth 500 because $10^{3-1}$ = 100 so at the third pass the value of the fraction is equal to 100 times the value of the digit at the third place. Adding all the terms together reverses the number 345 becomes 543, 1432 becomes 2341. If my explanation isn’t clear let me know!