该程序功能是当单击“开始”按钮时,求100到500间的质数并添加在列表框中。当单击“清除”时,清除列表框中的全部内容。
谢谢各位拉
Option Explicit
Sub Prime(n As Long)
Dim a() As Byte, b() As String
Dim i As Long, j As Long, l As Long, p As Long, q As Long
ReDim a(n + 6)
j = 1
l = Sqr(n)
Do
DoEvents
j = j + 4
If a(j) = 0 Then
i = j * j
p = j * 2
q = j * 4
Do While i < n
a(i) = 1
i = i + p
If i > n Then Exit Do
a(i) = 1
i = i + q
Loop
End If
j = j + 2
If a(j) = 0 Then
i = j * j
p = j * 2
q = j * 4
Do While i < n
a(i) = 1
i = i + q
If i > n Then Exit Do
a(i) = 1
i = i + p
Loop
End If
Loop Until j >= l
ReDim b(n \ 3 + 2)
b(0) = "2"
b(1) = "3"
j = 1
i = 1
Do
DoEvents
j = j + 4
If a(j) = 0 Then
i = i + 1
b(i) = j
End If
j = j + 2
If a(j) = 0 Then
i = i + 1
b(i) = j
End If
Loop Until j >= n
ReDim Preserve b(i)
For i = LBound(b) To UBound(b)
If b(i) > 100 And b(i) < 500 Then
List1.AddItem b(i)
End If
Next
End Sub
Private Sub Command1_Click()
Prime 500
End Sub
Private Sub Command2_Click()
List1.Clear
End Sub