Greatest Common Divisor

(Euclid's method)

A famous problem in the history of mathematics is called the Greatest Common Divisor problem. One solution to the problem is called "Euclid's Method" which is shown below in a JavaScript algorithm.

Algorithm (Euclid's method):


    W = initialValue1;    // for example, 48
    X = initialValue2;    // for example, 18   
    while (X != 0)
        {
            Z = W % X;
            W = X;
            X = Z;
        }    
    // when finished, W = GCD(W,X)

Pippin Assembly Language

LOD #NUMBER1
STO W
LOD #NUMBER2
STO X
LOD #0 (LOOP:)
STO T1
LOD X
SUB T1
STO T1
CPZ T1
NOT
JMZ END (54)
LOD X
STO T2
LOD W
STO T1
DIV T2
MUL T2
STO T2
LOD T1
SUB T2
STO Z
LOD X
STO W
LOD Z
STO X
JMP LOOP (8)
NOP (END:)
HLT