1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Invert Numbers

  1. Jul 17, 2006 #1
    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...
     
  2. jcsd
  3. Jul 17, 2006 #2

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    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: Jul 17, 2006
  4. Jul 17, 2006 #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...
     
  5. Jul 17, 2006 #4

    matt grime

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  6. Jul 17, 2006 #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...
     
  7. Jul 17, 2006 #6

    shmoe

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  8. Jul 17, 2006 #7

    matt grime

    User Avatar
    Science Advisor
    Homework Helper


    The factors of 11 are 1 and 11 so they are all palindromic... (Yes, I know you meant multiple.)
     
  9. Jul 17, 2006 #8
    oops, well spotted.... :redface:
     
  10. Jul 18, 2006 #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: Jul 19, 2006
  11. Jul 19, 2006 #10
    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: Jul 19, 2006
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Invert Numbers
  1. About Invert transform (Replies: 1)

  2. Inverted shape? (Replies: 4)

  3. Inverting a Series (Replies: 3)

Loading...