ご案内

ようこそ

水曜日, 6月 06, 2012

最小公倍数と最大公約数 (エクセル マクロ)


Sub 最大公約数()
Dim a As Long
Dim b As Long

a = Range("A1").Value
b = Range("B1").Value

Range("C1").Value = myGCD(a, b)
End Sub

Sub 最小公倍数()
Dim i As Long
Dim b As Long
Dim k As Long

a = Range("G1").Value
b = Range("H1").Value
k = a * b

Range("I1").Value = k / myGCD(a, b)
End Sub

Function myGCD(x As Long, y As Long) As Long
Do While x <> y
If x > y Then
x = x - y
Else
y = y - x
End If
Loop

myGCD = y
End Function


なるほど。

0 件のコメント: