第1个回答 2008-06-30
Private Sub Command1_Click()
Dim N As Long, Flags As Boolean
N = Val(Text1.Text)
Flags = True
For i = 2 To Sqr(N)
If N Mod i = 0 Then
Flags = False
Exit For
End If
Next
MsgBox N & IIf(Flags, "", "不") & "是一个素数"
End Sub本回答被提问者采纳
第2个回答 2008-06-30
Private Function s(t As Integer) As Boolean
If t = 1 Or t = 2 Then s = False: Exit Function
Else: s = True
End If
For i = 2 To t - 1
If t Mod i = 0 Then
s = False: Exit Function
End If
Next
End Function
'调用函数,如果函数为true即为素数,否则不是
第3个回答 2008-06-30
求100以内的素数:
Private Sub Form_click()
Dim i As Integer, m As Integer
Dim n As Integer
n = 1
j = 0
Do
m = Sqr(n)
For i = 2 To m
If n Mod i = 0 Then GoTo lp
Next i
Print n;
j = j + 1
lp: n = n + 1
Loop While n <= 100
End Sub