如何求素数???教教小弟
Private Sub Form_Click()
n = Val(Text1.Text)
Dim I As Integer
For I = 2 To Sqr(n)或者n/2
If n Mod I = 0 Then Exit For
Next I
If I = ???????????????????????????????????? Then
MsgBox Str(n) + "是素数 "
Else
MsgBox Str(n) + "不是素数 "
End If
End Sub
[解决办法]
'
Private Sub Command1_Click()
Dim n As Integer
n = Val(Text1.Text)
If n = 0 Then
MsgBox "ÊäÈë²»ÕýÈ· "
Text1.Text = " "
Exit Sub
End If
k = Int(Sqr(n))
i = 2
swit = 0
While i <= k And swit = 0
If n Mod i = 0 Then
swit = 1
Else
i = i + 1
End If
Wend
If swit = 0 Then
MsgBox n & "是素数 "
Else
MsgBox n & "不是素数 "
End If
End Sub
[解决办法]
Private Sub Form_Click()
n = Val(Text1.Text)
Dim I As Integer
Dim blSuShu As Boolean
For I = 2 To Sqr(n) '»òÕßn/2
If n Mod I = 0 Then
blSuShu = False
Exit For
Else
blSuShu = True
End If
Next I
If blSuShu Then
MsgBox Str(n) + "是素数 "
Else
MsgBox Str(n) + "不是素数 "
End If
End Sub
[解决办法]
哦,已经有解了
LZ,我仅是相当于解答了你问号部分,也可以运行。
daisy8675(莫依 俞小灏的姐姐团)的代码更充分。