'其实挺简单,求素数几乎是初学者的必修课。
'运行效果如上,代码如下所示。
'逐行手敲,亲测可用。
Private Sub Command1_Click()
Dim Inversion As Integer
Dim arr(1000) As Integer
Dim k, a As Integer
k = 1
For i = 1 To 1000
If IsPrimeNumber(i) Then
arr(k) = i '将1000以内的素数放入数组
k = k + 1
End If
Next i
Print "1000内的素数有:"; '把数组打印出来验证效果
Print
For a = 1 To k - 1
Print arr(a);
If a Mod 10 = 0 Then Print
Next a
End Sub
Public Function IsPrimeNumber(ByVal n As Integer) As Boolean
IsPrimeNumber = True
For i = 2 To Sqr(n)
If n Mod i = 0 Then
IsPrimeNumber = False
Exit Function
End If
Next i
End Function