(48G) GCD: Greatest Common Divisor

+- HP Forums (http://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (48G) GCD: Greatest Common Divisor (/thread-11296.html)



(48G) GCD: Greatest Common Divisor - Thomas Klemm - 08-30-2018 10:21 AM

This solution is from: HP 48 Insights Programs
Contains all the example programs from the book HP 48 Insights Part 1, by Bill Wickes.

INSIGHTS/PROGRAMS/GCD:
Code:
%%HP: T(3)A(D)F(.);
\<<
  WHILE DUP2 MOD
DUP 0 \=/
  REPEAT ROT DROP
  END ROT DROP2
\>>

However we can make it a bit shorter:
Code:
«
  WHILE DUP
  REPEAT SWAP OVER MOD
  END DROP
»

Example:

54 24
GCD

6



RE: (48G) GCD: Greatest Common Divisor - Werner - 08-31-2018 03:34 AM

Ow that's an old one. This is still quite a bit shorter:
Code:
\<<
  WHILE
    SWAP DUP2
  REPEAT
    MOD
  END
  DROP2
\>>

Cheers, Werner