第2个回答 2011-06-29
Public Sub iTest()
'求 x - y 之间的素数
Dim x, y, t As Boolean, i, j, k, res, msg$, n, tmp
x = 100 '最小值
y = 200 '最大值
For i = x To y
If i Mod 2 <> 0 Then
j = 3
k = Int(i ^ (0.5)) + 1
t = True
Do While j <= k
If i Mod j = 0 Then t = False: Exit Do
j = j + 2
Loop
If t Then res = res & " " & i
End If
Next
tmp = Split(Trim(res), " ")
n = UBound(tmp) + 1
msg = x & " - " & y & " 之间有 " & n & " 个素数:"
MsgBox msg & Chr(10) & Join(tmp, ",")
' Print msg & Chr(10) & Join(tmp, ",")
End Sub