Inverting Integer Numbers - Is There a Formula?

AI Thread Summary
The discussion centers on whether there is a mathematical formula to reverse the digits of an integer, as opposed to using an algorithm. A proposed function, f_n(x), defines the reversal of digits in base n, emphasizing that the base is crucial for the function's definition. Participants explore the properties of reversible strings and their relationships to factors like 11 and 9, noting that palindromic numbers arise from these factors. A symbolic formula for reversing digits in base b is suggested, though it is critiqued for its limitations and complexity. Ultimately, a simpler formula for R_b(n) is presented, aiming to provide a more straightforward method for digit reversal.
FenixDragon
Messages
2
Reaction score
0
hi... I was thinking... is there any formula that inverts int numbers? like 21 transforms into 12... I have found an algorithm that do this... but I want to know if exists any formula to it... thx...
 
Mathematics news on Phys.org
I will assume that you know both the given number x and the number of digits in x, n.

integer function Reverse(integer x, y)
{
y= 0; //comment: we will add parts of the answer to y
for i=1 to n
{
k= x/10^{n-i};
//comment: this is computer "integer" arithmetic : 3/2= 1, not 1.5
//comment: mathematically, k= (x- (x mod 10^{n-i}))/10^{n-i}
//comment: if x= 251, for example, n= 3, i=1, 10^{n-1}= 10^2= 100
//comment: then k= 251/100= 2 or k= (251- (251 mod 100))/100= (251- 51)/100= 2
y=y+ k*10^{i-1}
x= x- k;
}
return y;
}

With x= 251, n= 1, the first time through the loop, i= 1, k= 251/10^(3-1)= 251/100= 2 so y= 0+ 2*10^0= 2, x= 251- 200. The second time through the loop, i= 2, x= 51 now, k= 51/10^(3-2)= 51/10= 5 so y= 2+ 5*10= 52, x= 51-50. The third and last time through the loop, i= 3, x= 1, k= 1/10^(3-3)= 1/1= 1 and y= 52+ 1*100= 152, 251 reversed.

If you are given only x, not the number of digits in x, then you can find it this way.

n= 0;
k= 1;
while x>= k do
{
n= n+1
k= k*10
}

For example, if x= 251 again, we first see that 251> 1 so n= 0+ 1= 1. k= 1*10= 10. Since 251> 10, n= 1+1= 2, k= 10*10= 100. Since 251> 100, n= 2+ 1= 3, k= 10*100= 1000. Since 251< 1000, the loop terminates with n= 3, the number of digits in 251.
 
Last edited by a moderator:
sorry... but isn't exactly what I wanted... I already know this algorithm... but I want to know if exists any math function that do this... thx...
 
Yes, here is the function:

f(x)= "x written in reverse order of its digits".

That is a function. Actually, we had better call this f_10, otherwise it is not a well defined function on the integers: it depends on the choice of base, so let f_n(x)="x written in reverse order of its digits in base n".

I suspect your conflating the notion of function with a method of evaluating that function on some given input.
 
The base is important of course, because the string of digits is defined by the base you count in.

In base 10, the numbers 11 and 9 are important for reversable strings, factors of 11 tend be palindromic provided each of the digits is no greater than the base n/2. The difference between two mirror strings can be factored by 3^3.

Thats about all i know.

21 - 12 = 9
42 - 24 = 18
31 - 13 = 18
321 - 123 = 198 = 2 * 99 = 2*11*9

Think about the repeating decimal 0.99999...
 
Yes, the age old problem of what do you mean by "formula"?


I'd stick with the base 2 version of this function. You can then just turn the page upside down, or look in a mirror.
 
3trQN said:
In base 10, the numbers 11 and 9 are important for reversable strings, factors of 11 tend be palindromic


The factors of 11 are 1 and 11 so they are all palindromic... (Yes, I know you meant multiple.)
 
oops, well spotted... :redface:
 
Would this work as a symbolic formula for Rb(n), giving the number formed by reversing the digits of n in base b, when n is an integer?

R_b(n) = \sum_{k=1}^{\left \lceil log_b n \right \rceil} \left \lfloor \frac{n-b^k\left \lfloor \frac{n}{b^k} \right \rfloor}{b^{k-1}} \right \rfloor b^{\left \lceil log_b n \right \rceil - k}

Definitely could be simplified.
 
Last edited:
  • #10
gnomedt said:
Would this work as a symbolic formula for Rb(n), giving the number formed by reversing the digits of n in base b, when n is an integer?

R_b(n) = \sum_{k=1}^{\left \lceil log_b n \right \rceil} \left \lfloor \frac{n-b^k\left \lfloor \frac{n}{b^k} \right \rfloor}{b^{k-1}} \right \rfloor b^{\left \lceil log_b n \right \rceil - k}

Definitely could be simplified.
Your formula fails whenever n=bm for any natural 'm'.*

*Hint: the number of digits of any n \in \mathbb{N} in any base b \in \mathbb{N} - \left\{ {1} \right\} is \left\lfloor {\log _b n} \right\rfloor + 1, not \left\lceil {\log _b n}\right\rceil
(since you probably don't want to exclude natural powers of 'b' from your formula :wink:...)

---------------------------------------------------------
~Anyway, a simpler formula for Rb(n) is:

R_b (n) = \sum\limits_{i = 0}^{\left\lfloor {\log _b n} \right\rfloor } {\left( {\left\lfloor {\frac{n}{{b^i }}} \right\rfloor - b\left\lfloor {\frac{n}{{b^{i + 1} }}} \right\rfloor } \right)b^{\left\lfloor {\log _b n} \right\rfloor - i} }
 
Last edited:
Back
Top