|
最小公倍数を利用する方向でマクロを組んでみました。
a>b>cではなく、a<b<cになっちゃってますが。
Sub abc()
'宣言
Dim a, b, c, N1, N2, N, i As Long
'値
Cells(5, 1) = "a"
Cells(6, 1) = "b"
Cells(7, 1) = "c"
Cells(8, 1) = "N"
'総当り
For a = 2 To 10
For b = a + 1 To 10
For c = b + 1 To 10
N1 = LCM(a, b)
N2 = LCM(b, c)
N = LCM(N1, N2)
If N / a + N / b + N / c = N - 1 Then
Cells(5, 2 + i) = a
Cells(6, 2 + i) = b
Cells(7, 2 + i) = c
Cells(8, 2 + i) = N - 1
Cells(4, 2 + i) = i + 1 & "組目"
i = i + 1
End If
Next c
Next b
Next a
End Sub
Function GCD(ByVal a As Long, ByVal b As Long) As Long
If a * b = 0 Then GCD = 0: Exit Function
Dim x As Long
Do
x = a Mod b
a = b
b = x
Loop Until b = 0
GCD = a
End Function
Function LCM(ByVal a As Long, ByVal b As Long) As Long
If a * b = 0 Then LCM = 0: Exit Function
Dim x As Long
x = GCD(a, b)
LCM = a * b / x
End Function
|
|