The Greatest Common Divisor Program uses a WHILE loop to compute the greatest common divisor of two integers a and b. The remainder r = MOD(a,b) is calculated, b is replaced by a, and a is replaced by r until r=0.
To calculate the GCD of a and b, run the program and enter the two values. The result will be displayed in a message box.
| A | a |
| B | b |
| C | Copy of a |
| D | Copy of b, stores GCD result |
| R | Remainder |
Some characters on the HP 38G, like the store-arrow (\|>), are represented in the listing below by special three-character sequences. For a full list of these sequences, you can refer to the HP 38G Character Translation Codes page.
INPUT A;"GCD";"ENTER A:";;A: Get value for A
A\|>C: Copy A to C
INPUT B;"GCD";"ENTER B:";;B: Get value for B
B\|>D: Copy B to D
1\|>R: Initialize R
Calculate GCD using C and D:
WHILE
R\|/0 While R<>0
REPEAT
D MOD C \|>R: Calculate new R
C\|>D: Move C value to D
R\|>C: Move R to C
END:
Display result using original A & B values:
ERASE:
MSGBOX "GCD(" A "," B ")= " D
Back to James Donnelly's web site.
Back to the HP 38G page.
Back to the main calculator page.