求高手用VB语言编写一个求素数函数过程Prime(x),若x是素数返回True,否则返回False。

2024-11-14 11:23:28
推荐回答(2个)
回答1:

Private Function Prime(n As Integer) As Boolean
Dim k As Integer, Yes As Boolean
Yes=True
For k= 2 to Int(Sqr(n))
IF n mod k=0 Then Yes=False:Exit For
Next K
Prime=Yes
End Function
private sub form_click()
Dim N As Integer
N=Val(InputBox("输入一个正整数 N=?"))
if prime(N)=True Then
print N ;"是素数"
Else
print N;"不是素数"
End IF
End Sub

回答2:

Private Sub Command1_Click()
Dim x As Integer
x = Val(InputBox("请输入一个数以判断是否是素数"))
Prime (x)
End Sub

Private Function Prime(m As Integer)
Dim n As Integer
Dim i As Integer
n = Int(Sqr(m)) + 1
For i = 2 To n
If m Mod i = 0 Then
Exit For
End If
Next i
If i = n + 1 Then
Print m & "是素数"
Else
Print m & "不是素数"
End If
End Function