VBAで素数表を作る | 無線局 JP7FCS / ヤマガタST378

無線局 JP7FCS / ヤマガタST378

テーマ:アマチュア無線,デジタル簡易無線,オーボエ

資格:第二級アマチュア無線技士,第二級陸上特殊無線技士,測量士補,実用英語技能検定2級,漢字能力検定2級,数学検定2級

Excelで,Visual Basic Editor にペーストして実行してください!



Sub 素数表作成()

Cells.Select
Selection.ClearContents

Dim n As Long

n = CInt(InputBox("n="))

Dim i As Long
dim l as long
Dim k As Long
Dim s As Long

k = 1
Cells(k, 1).Value = "番号"
Cells(k, 2).Value = "素数"

k = 2
Cells(k, 1).Value = 1
Cells(k, 2).Value = 2

k = 3

For j = 3 To 2 * n - 1 Step 2

s = 0

For i = 3 To Int(Sqr(j)) Step 2

If j Mod i = 0 Then
s = s + 1
End If

Next i

If s < 1 Then
Cells(k, 1).Value = k - 1
Cells(k, 2).Value = j
k = k + 1
End If

Next j

Range("A1").Select

End Sub