Private Sub Command1_Click()
Dim i As Integer,x as Integer
For i =200 To 300
If prime(i) = True Then x=x+1
Next i
msgbox x
End Sub
Function prime(n As Integer) As Boolean '判断n是否为素数
Dim s As Boolean
Dim i As Integer
s = False
For i = 2 To Int(Sqr(n)) '如果能被2 ~Int(Sqr(n))中任何一个数整除,则不是素数,跳出For循环
If n Mod i = 0 Then s=true: Exit For
Next i
prime =not s '当i是素数时, prime=true,否则 prime=false 和 s 相反
End Function
Private Sub Form_Click()
Form1.AutoRedraw = True
Cls
Dim N As Integer, I As Integer
Print "200-300之间的素数有:"
For N = 200 To 300
If Prime(N) Then I = I + 1: Print N;
Next
Print
Print "共有"; I; "个"
End Sub
Function Prime(A As Integer) As Boolean
Dim X As Integer
Prime = True
For X = 2 To Sqr(A)
If A Mod X = 0 Then
Prime = False
Exit For
End If
Next
End Function