VB 输出10个随机数 判断是否是素数

来源:百度知道 编辑:UC知道 时间:2024/05/13 04:45:30

Private Sub Form_Load()
Dim i As Integer, j As Integer, n As Integer, v As Integer
For i = 1 To 10
v = 0
Randomize
n = Int(Rnd * 1000 + 1)
For j = 2 To n
If n Mod j = 0 Then
v = j
End If
Next j
If v > 0 Then
Print n & " 不是素数"
Else
Print n & " 是素数"
End If
Next i
End Sub

Private Sub Form_Click()
Dim i As Integer, j As Integer, n As Integer, v As Boolean
Randomize
For i = 1 To 10
v = True
n = Int(Rnd * 1000 + 1)
For j = 2 To Sqr(n)
If n Mod j = 0 Then
v = False
Exit For
End If
Next j
Print n & IIf(v, "", "不") & "是素数"
Next i
end sub