如何编写:求3~100之间所有的素数

2024-11-29 19:41:08
推荐回答(3个)
回答1:

Private Sub Form_Click()
Dim m As Integer, i As Integer, j As Integer

Print Space(2); 3; '3为素数,单独输出

For m = 5 To 100 Step 2 '从5开始计算,只需判断奇数,偶数肯定不是素数

For i = 3 To Sqr(m)
If m Mod i = 0 Then Exit For
Next i

If i > Sqr(m) Then
j = j + 1
If j Mod 10 = 0 Then Print
Print m;End If

Next m
End Sub

回答2:

For m = 5 To 100 Step 2 '从5开始计算,并且只计算奇数
For i = 3 To Sqr(m)
If m / i = Int(m / i) Then
heshu = True
Exit For
Else
heshu = False
End If
Next i
If heshu = False Then
print m;
End If
next m

回答3:

2008.03.08之前要结果~~~?????????什么意思啊