Post Reply 
(49G, 49g+ & 50g) ZREV: Speedily Reverse Digits of Integer
03-12-2018, 01:20 AM
Post: #2
RE: (49G, 49g+ & 50g) ZREV: Speedily Reverse Digits of Integer
That's a nice way to use →H, Gerald!

A couple of minor issues:
- Sign is lost
- 0 (exact integer zero) has a unique representation in memory, so the algorithm generates an error for that specific input

I hope you don't mind my offering an alternative. This is bigger (mostly due to the Saturn code block), but in addition to handling the above issues, it also:
- has overloaded handlers for lists and strings so that all three types of objects can be passed to it
- is a wee bit faster for exact integers Smile

Size: 108.5 bytes
Cksum: # 172Ah

Code:
::
   CK1NOLASTWD
   CK&DISPATCH1
   BINT255d ( zint ) ::
      TOTEMPOB
      CODE
         SAVE
         A=DAT1 A
         D1=A
         D1+5
         A=DAT1 A
         A-6 A
         C=0 A
         LC 2
         ?A<C A -> .Exit
         D1+5
         CD1EX
         D1=C
         C-1 A
         C+A A
         D0=C
         ASRB A
         A-1 A
         B=A A
         {
            A=DAT0 P
            C=DAT1 P
            DAT0=C P
            DAT1=A P
            D1+1
            D0-1
            B-1 A
            UPNC
         }
         *.Exit
         LOADRPL
      ENDCODE
      FPTR2 ^ZTrim
   ;
   list ::
      INNERCOMP
      reversym
      {}N
   ;
   str ::
      EvalNoCK:_ ROMPTR 100 F
   ;
;
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (49G, 49g+ & 50g) ZREV: Speedily Reverse Digits of Integer - DavidM - 03-12-2018 01:20 AM



User(s) browsing this thread: 2 Guest(s)