Function GCD(ByVal x As Integer, ByVal y As Integer) As Integer ' 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 Dim g As Integer ' Work with absolute values (positive integers) If x < 0 Then x = -x If y < 0 Then y = -y If x + y > 0 Then g = y ' Iterate until x = 0 While x > 0 g = x x = y Mod x y = g Wend Return g Else ' Error, both parameters zero Return 0 End If End Function
Hide code
Visustin flow chart for REALbasic