Post Reply 
(48G) GCD: Greatest Common Divisor
09-01-2018, 08:41 PM
Post: #19
RE: (48G) GCD: Greatest Common Divisor
(09-01-2018 07:40 PM)Thomas Klemm Wrote:  Thanks again for taking the time for this analysis!
Just out of curiosity: what would be the shortest SysRPL program to calculate the GCD of two numbers?

Everyone's least favorite answer:

"It depends..."

If the program is intended to behave in a "normal" fashion (accepting varying argument types, preserving stack for UNDO/LAST, etc.), I'm not sure you could save any space at all by going the SysRPL route.

If you don't care about error trapping or argument type checking (which leaves you exposed for a nasty crash if you forget which types of arguments to use), you could go with a "throw caution to the wind" approach and do something like this:
Code:
::
   BEGIN
      SWAPOVER
      DUP %0<>
   WHILE
      %MOD
   REPEAT
   ROT2DROP
;

This only works with approximate numbers, and weighs in at 25 bytes. Not much savings for the risk and hassle, IMHO. Then of course you'd need something else entirely if exact integers were involved, which makes going the SysRPL route even less attractive. I'd stick with a UserRPL version for something like this.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (48G) GCD: Greatest Common Divisor - DavidM - 09-01-2018 08:41 PM



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