Inverting Integer Numbers - Is There a Formula?

In summary, there is no single math formula for reversing the digits of an integer in any base. However, there are methods and algorithms that can be used to achieve this. One such algorithm is the integer function Reverse which uses a for loop to reverse the digits of an integer in base 10. Another method is to use a while loop to find the number of digits in the integer and then use that information to reverse the digits. Additionally, a symbolic formula has been proposed for reversing the digits in any base, but it has limitations and can be simplified. Ultimately, there is no one formula that can accurately and efficiently reverse the digits of an integer in any base.
  • #1
FenixDragon
2
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
  • #2
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:
  • #3
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...
 
  • #4
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.
 
  • #5
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...
 
  • #6
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.
 
  • #7
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.)
 
  • #8
oops, well spotted... :redface:
 
  • #9
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?

[tex]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}[/tex]

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?

[tex]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}[/tex]

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

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

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

[tex]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} } [/tex]
 
Last edited:

FAQ: Inverting Integer Numbers - Is There a Formula?

What is meant by "inverting integer numbers"?

Inverting integer numbers refers to the process of reversing the digits of a given integer. For example, the number 123 would become 321 when inverted.

Is there a formula for inverting integer numbers?

Yes, there is a formula for inverting integer numbers. It is the product of the number's digits multiplied by their respective place values. For example, to invert the number 123, the formula would be (1*100) + (2*10) + (3*1) = 321.

Can all integer numbers be inverted?

Yes, all integer numbers can be inverted. However, the resulting number may not always be an integer, depending on the number of digits and the presence of leading zeros.

Why would someone want to invert an integer number?

Inverting integer numbers can be useful in certain mathematical operations, such as finding the inverse of a number in modular arithmetic. It can also be used in coding and data manipulation.

Are there any restrictions or limitations when inverting integer numbers?

There are no specific restrictions or limitations when inverting integer numbers. However, the resulting number may be too large to store in certain data types, so it is important to consider the data type when working with large integers.

Back
Top