Least Common Multiple

By James Donnelly


The Least Common Multiple Program first 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. The program then uses the formula m=a*b/GCD(a,b) to calculate the least common multiple of the two integers.

To calculate the LCM of a and b, run the program and enter the two values. The result will be displayed in a message box.

LCM Variables
Variable
Description
Aa
Bb
CCopy of a
DCopy of b,
stores GCD result
LLCM result
RRemainder

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:
B/D*A\|>L                       Calculate LCM

Display result using original A & B values:

ERASE:
MSGBOX "LCM(" A "," B ")= " L

Back to James Donnelly's web site.
Back to the HP 38G page.
Back to the main calculator page.