function GCD(x, y) /* The greatest common denominator (GCD) is the largest positive integer that divides into both numbers without a remainder. Examples: GCD(256,64)=64, GCD(12,8)=4, GCD(5,3)=1 */ { var g; /* Work with absolute values (positive integers) */ if (x < 0) x = -x; if (y < 0) y = -y; if ((x+y) == 0) return 0 /* Error, both parameters zero */ g = y; /* Iterate until x = 0 */ while(x > 0) { g = x; x = y % x; y = g; } return g }
Hide code
Visustin flow chart for JavaScript