Attribute VB_Name = "GCDModule"
Option Explicit

Public Function GCD(ByVal x As Long, ByVal y As Long) As Long
' 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 Long

' 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
Do While x > 0
g = x
x = y Mod x
y = g
Loop
GCD = g
Else
' Error, both parameters zero
GCD = 0
End If

End Function