Reverse a number without using strings

Share your advanced PureBasic knowledge/code with the community.
User avatar
jacdelad
Addict
Addict
Posts: 2044
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Reverse a number without using strings

Post by jacdelad »

Code: Select all

Procedure RevNumber(num_in.q)
  Protected num_out.q=0
  While num_in<>0
    num_out=num_out*10
    num_out=num_out+num_in%10
    num_in=num_in/10
  Wend
  ProcedureReturn num_out
EndProcedure

Debug RevNumber(123456789);shows 987654321
Debug RevNumber(123654789);shows 987456321
Last edited by jacdelad on Sat Jul 23, 2022 7:16 am, edited 1 time in total.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4269
Joined: Thu Apr 18, 2019 8:17 am

Re: Reverse a number without using strings

Post by BarryG »

Cute. No practical use though? Very slow compared to just reversing a string. Also doesn't handle numbers ending in a zero, or floats.

Code: Select all

Procedure RevNumber(num_in.q)
  Protected num_out.q=0
  While num_in<>0
    num_out=num_out*10
    num_out=num_out+Mod(num_in,10)
    num_in=num_in/10
  Wend
  ProcedureReturn num_out
EndProcedure

DisableDebugger

test1_start.q=ElapsedMilliseconds()
For c=1 To 1000000
  r=RevNumber(123456789)
Next
test1_time.q=ElapsedMilliseconds()-test1_start

n$="123456789"
test2_start.q=ElapsedMilliseconds()
For c=1 To 1000000
  r$=ReverseString(n$)
Next
test2_time.q=ElapsedMilliseconds()-test2_start

EnableDebugger

Debug test1_time ; 1663 ms
Debug test2_time ; 119 ms
User avatar
jacdelad
Addict
Addict
Posts: 2044
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Reverse a number without using strings

Post by jacdelad »

Interesting, I thought it would be faster than using a string, maybe Mod() is slow.
Technically the stored reverse value of a number ending with zeroes, like 1000, is correct. You just have to make sure to add the trailing zeroes when converting it to a string.
There is no real practical use, I just liked the approach. Including floats would be more complex.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4269
Joined: Thu Apr 18, 2019 8:17 am

Re: Reverse a number without using strings

Post by BarryG »

It's a good trick, though.
User avatar
STARGÅTE
Addict
Addict
Posts: 2266
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Reverse a number without using strings

Post by STARGÅTE »

Mod(x,y) is a floating point function (for floats and doubles) and not useful for integers, try:

Code: Select all

RevNumber(123456789123456789)
You have to use the equivalent integer operator %

Code: Select all

num_out = num_out + num_in % 10
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
jacdelad
Addict
Addict
Posts: 2044
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Reverse a number without using strings

Post by jacdelad »

@STARGATE: Aye great, didn't mention that it is for doubles/floats.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply