Author Topic: Number inversion  (Read 6895 times)

0 Members and 1 Guest are viewing this topic.

Offline joao9856

  • LV3 Member (Next: 100)
  • ***
  • Posts: 73
  • Rating: +3/-0
    • View Profile
Number inversion
« on: November 13, 2012, 01:17:45 pm »
Is it possible in Ti Nspire by any mean to transform a number into its reverse like this:
EX: 103 --> 301?

Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: Number inversion
« Reply #1 on: November 13, 2012, 01:24:19 pm »
Creating a program (either with Program Editor or LUA) should be possible to mirror the numbers

Offline joao9856

  • LV3 Member (Next: 100)
  • ***
  • Posts: 73
  • Rating: +3/-0
    • View Profile
Re: Number inversion
« Reply #2 on: November 13, 2012, 01:27:28 pm »
Thats the idea but i have no clue how to start the program really i'm blind here...

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Number inversion
« Reply #3 on: November 13, 2012, 01:28:18 pm »
This will work regardless of language:

Final = 0
Reps = int(log(Num))
for (A, 0, Reps)
    Next = int(Num / 10)
    Final = Final * 10 + Num - 10*Next
    Num = Next
« Last Edit: November 13, 2012, 01:38:56 pm by thepenguin77 »
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Number inversion
« Reply #4 on: November 13, 2012, 01:35:10 pm »
Your program adds a zero to the end of the number penguin.

Some people need a high five in the face... with a chair.
~EC

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Number inversion
« Reply #5 on: November 13, 2012, 01:39:16 pm »
Your program adds a zero to the end of the number penguin.

I guess it all depends on how your for loop works. But I fixed it to work on calculators.
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Number inversion
« Reply #6 on: November 13, 2012, 01:48:57 pm »
Your program adds a zero to the end of the number penguin.
I guess it all depends on how your for loop works. But I fixed it to work on calculators.
Maybe it differs from platform to platform? Anyway it can easily be cut off by dividing final by 10 when done.
On topic, here's your program in it's entire form joao9856:
Program style:
Code: [Select]
Define reverse(num)=
Prgm
    Local final,reps,next,a
    final:=0
    reps:=int(log(num,10))+1
    For a,0,reps
        next:=int(((num)/(10)))
        final:=final*10+num-10*next
        num:=next
    EndFor
    result = final/10
    Disp result
EndPrgm

Function style:
Code: [Select]
Define reversefunc(num)=
Func
Local final,reps,next,a
final:=0
reps:=int(log(num,10))+1
For a,0,reps
    next:=int(((num)/(10)))
    final:=final*10+num-10*next
    num:=next
EndFor
Return ((final)/(10))
EndFunc

If you just want to print the reversed number, use the program. If you want to perform arithmetic or something else with the reversed number use the function.
« Last Edit: November 13, 2012, 01:55:13 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline joao9856

  • LV3 Member (Next: 100)
  • ***
  • Posts: 73
  • Rating: +3/-0
    • View Profile
Re: Number inversion
« Reply #7 on: November 13, 2012, 01:56:32 pm »
thanks guys!

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Number inversion
« Reply #8 on: November 13, 2012, 01:58:46 pm »
No problem :) Also welcome to omni  did you introduce yourself to get your free bag  of !peanuts?
Heh, I should thank you too penguin. Just found out it comes in very handy in my project :P
« Last Edit: November 13, 2012, 02:01:13 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: Number inversion
« Reply #9 on: November 13, 2012, 02:07:35 pm »
I just want to point out that since you're working with numbers (and not strings), any leading zeros won't come into effect (e.g. reverse(00123) will get you "321" and not "32100" (though the zeros disappear the moment you hit enter).

Also, that program will throw an error with numbers less or equal than 0 (because of log), but I suppose that's not part of the problem of mirroring numbers...

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Number inversion
« Reply #10 on: November 13, 2012, 02:16:00 pm »
Oh you're right someone... I'll have a look into this tonight to find out what string manipulations tools we have again.

Some people need a high five in the face... with a chair.
~EC

Offline Rhombicuboctahedron

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 437
  • Rating: +41/-6
    • View Profile
Re: Number inversion
« Reply #11 on: November 13, 2012, 03:30:32 pm »
Code: [Select]
Define reverse()=
Prgm
    Local reverse,n,a
    RequestStr “ ”,a
    n:=dim(a)
   reverse:=mid(a,n,1)
    For x,1,n-1
    reverse:= reverse& reverse:=mid(a,n-x,1)
    EndFor
    reverse:=expr(reverse)
    Disp reverse
EndPrgm
Alternatively, you could just type the string into the parentheses at the beginning, with the quotation marks
Also, you could output as a string, by removing the reverse:=expr(reverse), this won’t work with 321000

Offline joao9856

  • LV3 Member (Next: 100)
  • ***
  • Posts: 73
  • Rating: +3/-0
    • View Profile
Re: Number inversion
« Reply #12 on: November 15, 2012, 11:34:29 am »
Is there any way of making this code run faster(i'm kinda newb here!)
Code: [Select]
Define LibPub inversor()=
Prgm
:Local final,reps,next,a
:f:=103
:Lbl g
:num:=f
:final:=0
:reps:=int(log(num,10))+1
:For a,0,reps
:next:=int(((num)/(10)))
:final:=final*10+num-10*next
:num:=next
:EndFor
:resulti:=((final)/(10))
:num:=resulti-f
:l:=num
:Disp "O Inverso de ",f," é ",resulti," logo:"
:Disp resulti,"-",f,"=",l
:Local final,reps,next,a
:final:=0
:reps:=int(log(num,10))+1
:For a,0,reps
:next:=int(((num)/(10)))
:final:=final*10+num-10*next
:num:=next
:EndFor
:resultii:=((final)/(10))
:Disp "O inverso de ",l," é ",resultii," logo:"
:Disp resultii,"+",l,"=",resultii+l
:Lbl e
:f:=f+1
:If f≥998 Then
:Goto h
:ElseIf f≥103 and f≤109 Then
:Goto g
:ElseIf f≥113 and f≤119 Then
:Goto g
:ElseIf f≥123 and f≤129 Then
:Goto g
:ElseIf f≥133 and f≤139 Then
:Goto g
:ElseIf f≥143 and f≤149 Then
:Goto g
:ElseIf f≥153 and f≤159 Then
:Goto g
:ElseIf f≥163 and f≤169 Then
:Goto g
:ElseIf f≥173 and f≤179 Then
:Goto g
:ElseIf f≥183 and f≤189 Then
:Goto g
:ElseIf f≥193 and f≤199 Then
:Goto g
:ElseIf f≥204 and f≤209 Then
:Goto g
:ElseIf f≥214 and f≤219 Then
:Goto g
:ElseIf f≥224 and f≤229 Then
:Goto g
:ElseIf f≥234 and f≤239 Then
:Goto g
:ElseIf f≥244 and f≤249 Then
:Goto g
:ElseIf f≥254 and f≤259 Then
:Goto g
:ElseIf f≥264 and f≤269 Then
:Goto g
:ElseIf f≥274 and f≤279 Then
:Goto g
:ElseIf f≥284 and f≤289 Then
:Goto g
:ElseIf f≥294 and f≤299 Then
:Goto g
:ElseIf f≥305 and f≤309 Then
:Goto g
:ElseIf f≥315 and f≤319 Then
:Goto g
:ElseIf f≥325 and f≤329 Then
:Goto g
:ElseIf f≥335 and f≤339 Then
:Goto g
:ElseIf f≥345 and f≤349 Then
:Goto g
:ElseIf f≥355 and f≤359 Then
:Goto g
:ElseIf f≥365 and f≤369 Then
:Goto g
:ElseIf f≥375 and f≤379 Then
:Goto g
:ElseIf f≥385 and f≤389 Then
:Goto g
:ElseIf f≥395 and f≤399 Then
:Goto g
:ElseIf f≥406 and f≤409 Then
:Goto g
:ElseIf f≥416 and f≤419 Then
:Goto g
:ElseIf f≥426 and f≤429 Then
:Goto g
:ElseIf f≥436 and f≤439 Then
:Goto g
:ElseIf f≥446 and f≤449 Then
:Goto g
:ElseIf f≥456 and f≤459 Then
:Goto g
:ElseIf f≥466 and f≤469 Then
:Goto g
:ElseIf f≥476 and f≤479 Then
:Goto g
:ElseIf f≥486 and f≤489 Then
:Goto g
:ElseIf f≥496 and f≤499 Then
:Goto g
:ElseIf f≥507 and f≤509 Then
:Goto g
:ElseIf f≥517 and f≤519 Then
:Goto g
:ElseIf f≥527 and f≤529 Then
:Goto g
:ElseIf f≥537 and f≤539 Then
:Goto g
:ElseIf f≥547 and f≤549 Then
:Goto g
:ElseIf f≥557 and f≤559 Then
:Goto g
:ElseIf f≥567 and f≤569 Then
:Goto g
:ElseIf f≥577 and f≤579 Then
:Goto g
:ElseIf f≥587 and f≤589 Then
:Goto g
:ElseIf f≥597 and f≤599 Then
:Goto g
:ElseIf f≥608 and f≤609 Then
:Goto g
:ElseIf f≥618 and f≤619 Then
:Goto g
:ElseIf f≥628 and f≤629 Then
:Goto g
:ElseIf f≥638 and f≤639 Then
:Goto g
:ElseIf f≥648 and f≤649 Then
:Goto g
:ElseIf f≥658 and f≤659 Then
:Goto g
:ElseIf f≥668 and f≤669 Then
:Goto g
:ElseIf f≥678 and f≤679 Then
:Goto g
:ElseIf f≥688 and f≤689 Then
:Goto g
:ElseIf f≥698 and f≤699 Then
:Goto g
:ElseIf f=709 Then
:Goto g
:ElseIf f=719 Then
:Goto g
:ElseIf f=729 Then
:Goto g
:ElseIf f=739 Then
:Goto g
:ElseIf f=749 Then
:Goto g
:ElseIf f=759 Then
:Goto g
:ElseIf f=769 Then
:Goto g
:ElseIf f=779 Then
:Goto g
:ElseIf f=789 Then
:Goto g
:ElseIf f=799 Then
:Goto g
:Else
:Goto e
:EndIf
:Lbl h
:EndPrgm

The language is portuguese and its says like this:
301 is the mirror for 103 then:
301-103=198
891 is the mirror for 198 then:
891+198=1089
 and so on..

Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6266
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: Number inversion
« Reply #13 on: November 15, 2012, 11:40:14 am »
I would condense all your if/elseif <condition> goto g's into one large if statement. So
Code: [Select]
if <contition> or <condidtion> ...
goto g
else
goto e
end
I would also suggest not using goto/label, they can cause memory leaks in most languages.
/e

Offline joao9856

  • LV3 Member (Next: 100)
  • ***
  • Posts: 73
  • Rating: +3/-0
    • View Profile
Re: Number inversion
« Reply #14 on: November 15, 2012, 11:43:42 am »
so how do i avoid goto/label?