Dim int_m1 As Integer '計算対象範囲(最小) Dim int_m2 As Integer '計算対象範囲(最大) Dim int_X1 As Integer '左辺 項1 Dim int_X2 As Integer '左辺 項2 Dim int_X3 As Integer '右辺 Dim int_cnt1 As Integer 'ループカウンタ1 Dim int_cnt2 As Integer 'ループカウンタ2
int_m1 = '何でもいいから入力値を取得 int_m2 = '何でもいいから入力値を取得
'入力値チェック関数 If f_ChkBaseNumberError(int_m1, int_m2) Then Return End If
'ピタゴラス数を計算 For int_cnt1 = int_m1 To int_m2 For int_cnt2 = 1 To int_cnt1 - 1 'X1 : (d * (m^2 - n^2)) / d -> m^2 - n^2 int_X1 = int_cnt1 ^ 2 - int_cnt2 ^ 2 'X2 : (d * 2 * m * n) / d -> 2 * m * n int_X2 = 2 * int_cnt1 * int_cnt2 'X3 : (d * (m^2 + n^2)) / d int_X3 = int_cnt1 ^ 2 + int_cnt2 ^ 2
'最小公約数を計算 'この関数は最小公約数が1以外のとき true を返す If f_ChkDiv(int_X1, int_X2, int_X3) Then Me.TextBox1.AppendText("X") End If
Private Function f_ChkDiv(ByVal d1 As Integer, ByVal d2 As Integer, ByVal d3 As Integer) As Boolean Dim int_cnt1 As Integer
'引数を昇順ソート Dim ar() As Integer = {d1, d2, d3} Array.Sort(ar) f_ChkDiv = False
'ar(0) 引数のうち一番小さい値 '2 <= int_cnt1 <= ar(0) 'ar(0) が int_cnt1 割り切れたら順に ar(1) ar(2) が割り切れるか調べる For int_cnt1 = 2 To ar(0) If (ar(0) Mod int_cnt1) = 0 Then If (ar(1) Mod int_cnt1) = 0 Then If (ar(2) Mod int_cnt1) = 0 Then f_ChkDiv = True Exit For End If End If End If Next End Function
Sub abc() Dim a As Integer, b As Integer, c As Integer For a = 1 To 50 For b = 1 To 50 For c = 1 To 50 If a > b Then Exit For ElseIf prim(a, b) = 0 Then Exit For ElseIf a ^ 2 + b ^ 2 = c ^ 2 Then MsgBox "(" & a & "," & b & "," & c & ")" & a ^ 2 & "+" & b ^ 2 & "=" & c ^ 2 End If
Next c Next b Next a
End Sub
Function prim(f, g) If 1 = gcd(f, g) Then prim = 1 Else prim = 0 End If
End Function
Function gcd(f, g) Dim h As Integer, s As Integer, r As Integer h = f s = g Do While s > 1 r = h Mod s h = s s = r Loop gcd = r End Function