Function GCD(x, y : LongInt) : LongInt; { 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 : LongInt; Begin { Work with absolute values (positive integers) } If x < 0 Then x := -x; If y < 0 Then y := -y; If x + y > 0 Then Begin g := y; { Iterate until x = 0 } While x > 0 Do Begin g := x; x := y Mod x; y := g; End; GCD := g; End Else Begin { Error, both parameters zero } GCD := 0; End; End;
Hide code
Visustin flow chart for Pascal