VB判断质数

来源:百度知道 编辑:UC知道 时间:2024/06/30 17:18:49
Private Sub Command1_Click()
Dim a As Integer
Dim n As Integer
Dim i As Integer
i = 2
a = Val(Text1.Text) Mod Sqr(i)
i = i + 1
If i > Val(Text1.Text) - 1 Or a = 0 Then
Text2.Text = "这不是一个质数"
Else
Text2.Text = "这是一个质数"
End If
End Sub
我想判断质数,该咋改啊?
有2个text,text1用来输入,text2用来输出。

Private Sub Command1_Click()
Dim a As Integer
Dim k As Integer
Dim i As Integer
i = 2
a = Val(Text1.Text)
do
k=a mod i
i+=1
loop while i<a
if k=0 then text2.text="你输入的不是个素数"
else text2.text="你输入的是一个素数"Text2.Text = "这不是一个质数"
Else
Text2.Text = "这是一个质数"
End If
End Sub

Private Function Pn(I_s As Integer) As Boolean
Pn = True
Dim i As Integer
i = 2
If I_s >= 2 Then
Do While i * i <= I_s
If I_s \ i = I_s / i Then
Pn = False
Exit Do
End If
i = i + 1
Loop
Else
Pn = False
End If
End Function
使用方式:
if Pn(整数) then
Msgbox "这是一个质数!"
else
Msgbox "这不是一个质数!"
endif

2个text,text1用来输入,text2用来输出。Private Sub Command1_