Is there a mathematical way to reverse digits of an integer mathematically? for example: $18539 \rightarrow 93581$
-
You mean like division? – walcher Aug 30 '13 at 16:40
-
1Like $18539$ becomes $93581$, I would guess. But what should happen if you reverse $10$? – Henry Swanson Aug 30 '13 at 16:42
-
3What does 'reverse an integer' mean? – copper.hat Aug 30 '13 at 16:43
-
7You could view every number as a polynomial $p(x)$ with non-negative integer coefficients evaluated at $10$, where the $n^{\text{th}}$ digit (from the right) is the coefficient of $x^n$. Then its reverse is $10^mp(\frac 1{10})$, where $m=\text{deg};p$. This would revert $10$ into $1$. – walcher Aug 30 '13 at 16:48
-
you mean reverse the digits? – rurouniwallace Aug 30 '13 at 16:53
-
@copper.hat I interpret your comment as: 1 should be reversed to 1 or should be reversed to 01 to 10. Thus, reversing is not a function unless some other conditions are put. – Amr Aug 30 '13 at 20:12
3 Answers
Let $n = \lfloor \log_{10}x\rfloor$, then $x$ has $n + 1$ digits; see this answer.
Let $x = \displaystyle\sum_{i=0}^nx_i10^i$ where $x_i \in \{0, 1, \dots, 9\}$. To obtain $x_k$ (i.e. the $(k+1)^{\text{st}}$ digit of $x$), first divide by $10^k$ to obtain
$$x10^{-k} = \sum_{i=0}^nx_i10^{i-k} = \sum_{i=0}^{k-1}x_i10^{i-k} + \sum_{i=k}^nx_i10^{i-k}.$$
Note that the first sum on the right hand side is a non-negative number less than $1$ and the second sum is an integer. Therefore
$$\lfloor x10^{-k}\rfloor = \left\lfloor\sum_{i=0}^nx_i10^{i-k}\right\rfloor = \sum_{i=k}^nx_i10^{i-k}.$$
Now consider the corresponding expression for $k+1$:
\begin{align*} \lfloor x10^{-(k+1)}\rfloor &= \left\lfloor\sum_{i=0}^nx_i10^{i-(k+1)}\right\rfloor\\ &= \sum_{i=k+1}^nx_i10^{i-(k+1)}\\ &= 10^{-1} \sum_{i=k+1}^nx_i10^{i-k}\\ &= 10^{-1}\left(\sum_{i=k}^nx_i10^{i-k}-x_k\right)\\ &= 10^{-1}\left(\lfloor x10^{-k}\rfloor - x_k\right). \end{align*}
Rearranging for $x_k$ we find $x_k = \lfloor x10^{-k}\rfloor - 10\lfloor x10^{-(k+1)}\rfloor$. Therefore, the number you are after is
\begin{align*} \sum_{k=0}^nx_{n-k}10^k &= \sum_{k=0}^n\left(\lfloor x10^{-(n-k)}\rfloor - 10\lfloor x10^{-(n-k+1)}\rfloor\right)10^k\\ &= \sum_{k=0}^n\lfloor x10^{-(n-k)}\rfloor10^k - \sum_{k=0}^n\lfloor x10^{-(n-k+1)}\rfloor10^{k+1} \end{align*}
or if you prefer
\begin{align*} \sum_{k=0}^nx_k10^{n-k} &= \left(\lfloor x10^{-k}\rfloor - 10\lfloor x10^{-(k+1)}\rfloor\right)10^{n-k}\\ &= \sum_{k=0}^n\lfloor x10^{-k}\rfloor10^{n-k} - \sum_{k=0}^n\lfloor x10^{-(k+1)}\rfloor10^{n-k+1}. \end{align*}
We can simplify this even further. Note that $\lfloor x10^{-n+1}\rfloor = 0$ so the final term in the second sum is zero. Now shift the index in the second sum so that it begins at $k = 1$:
\begin{align*} &\sum_{k=0}^n\lfloor x10^{-k}\rfloor10^{n-k} - \sum_{k=0}^n\lfloor x10^{-(k+1)}\rfloor10^{n-k+1}\\ &= \sum_{k=0}^n\lfloor x10^{-k}\rfloor10^{n-k} - \sum_{k=1}^n\lfloor x10^{-k}\rfloor10^{n-k+2}\\ &= \lfloor x10^0\rfloor10^n + \sum_{k=1}^n\lfloor x10^{-k}\rfloor10^{n-k} - 100\sum_{k=1}^n\lfloor x10^{-k}\rfloor10^{n-k}\\ &= x10^n - 99\sum_{k=1}^n\lfloor x10^{-k}\rfloor10^{n-k}. \end{align*}
Therefore, given a positive integer $x$, the integer which has the same digits in the reverse order is $$x10^{\lfloor \log_{10}x\rfloor} - 99\sum_{k=1}^{\lfloor \log_{10}x\rfloor}\lfloor x10^{-k}\rfloor10^{\lfloor \log_{10}x\rfloor-k}.$$
Here's an example calculation. Let $x = 123$, then $\lfloor \log_{10}x\rfloor = 2$. As $\lfloor x10^{-1}\rfloor = \lfloor 12.3\rfloor = 12$ and $\lfloor x10^{-2}\rfloor = \lfloor 1.23\rfloor = 1$, we have
$$123\times 10^2 - 99\left(12\times 10 + 1\times 1\right) = 123\times 100 - 99\times 121 = 12300 - 11979 = 321$$
as expected.

- 99,526
Here's a recursive formula:
$$R(N)=U(N)10^{[log(N)]}+R([N/10])$$
where
$$U(N)=N-10[N/10]$$
is the "unit" digit of $N$ (i.e., the digit in the "ones" column), and $R(0)$ is understood to equal $0$. The logarithm is base $10$, and the square brackets indicate the greatest-integer (floor) function.
Note: reversing digits is not an involution when $U(N)=0$. E.g., $R(100)=1$, but R($1)\neq100$.
Added later: It occurs to me that the recursive formula adapts nicely to digit reversal in any base:
$$R_b(N) = U_b(N)b^{[log_b(N)]}+R_b([N/b])$$
where
$$U_b(N)=N-b[N/b]$$

- 79,832
let number be 123.
perform 123%10 = 3 = a
perform 123/10 =12
if not 0,then multiply a with 10, so a = 30
perform 12%10 = b
perform 12/10 = 1
if not 0, then multiply b with 10 & a with 10,so a = 300 & b = 20
perform 1%10 = c
perform 1/10 = 0
if not 0, continue....however, as this is 0, you stop the process.
just add a+b+c. = 300+20+1 = 321
& in terms of pure mathematics:
sum of [{floor of n/10^(N-1-k)}*{10^k] for k= 0 to N-1. where n is integer, & N is no of digits.

- 247
-
% stands for modulo operation, & result of divide by 10 is an integer,not a fraction. – Sumedh Aug 30 '13 at 16:59
-
I can't be certain (given the scant amount of information the OP gave), but I think the asker was looking for a single closed form equation rather than an algorithm. – rurouniwallace Aug 30 '13 at 16:59
-
sum of [{floor of n/10^(N-1-k)}*{10^k] for k= 0 to N-1.
where n is integer, & N is no of digits.
– Sumedh Aug 30 '13 at 17:11 -
-
4
-
I gave my reason above, but in addition to that, your symbols and math aren't correct. For example, $12/10$ does not equal $1$, and in mathematics, $%$ does not work the way that you're using it. – rurouniwallace Aug 30 '13 at 17:18
-
@Ataraxia. In computer-speak, it's often the case that for positive integers $n, m$ the expression $\mathtt{n/m}$ denotes what mathematicians would call $\lfloor n/m\rfloor$. – Rick Decker Aug 30 '13 at 18:06
-
-
-
I think its fine to use any symbol as long as you are clear on what it means. So its ok to use $%$ the way Sumedh did but may be he should have stated how he was using it. I think most people who know math has had exposure to at least a bit of computer science so its not such a big deal in my opinion. – Pratyush Sarkar Aug 30 '13 at 19:10
-
@Ataraxia, I already told about % & divide by 10 results into an integer, in my first comment,before anyone of you pointed that out. However,I do admit I was wrong in the way I wrote the answer,but I do hope that instead of voting down an answer, you'll allow the poster to edit it. Vote down is done when the answer is wrong, which wasnt the case here. – Sumedh Aug 31 '13 at 10:42