在VB中用方程式表示M,N两个数的最大公因子

来源:百度知道 编辑:UC知道 时间:2024/05/29 05:55:11
用方程式表示M,N的最大公因子

Private Sub Command1_Click()
Dim m As Long, n As Long
n = Val(Text1.Text)
m = Val(Text2.Text)
If m * n = 0 Then
MsgBox "两数不能为0!"
Else
MsgBox Text1.Text & "," & Text2.Text & "最大公约数(因子)为" & aa(m, n)
End If
End Sub
Private Function aa(m As Long, n As Long) As Long
Dim r As Long

Do
r = m Mod n
m = n: n = r
Loop While r <> 0
aa = m
End Function

Function gcd(byval x as integer,byval y as integer) as integer
dim reminder as integer
do while y<>0
reminder=x mod y
x=y
y=reminder
loop
gcd=x
end function