private sub command1_click()
dim n as Long
dim Nss as boolean
n=val(trim(text1.text))
if ss(n)=True then
text2.text="N=" & n & " 是素数"
else
do while Nss=True
n=n+1
Nss=ss(N)
loop
text1.text=n
text2.text="N=" & n & " 是素数"
endif
end sub
或者把这段代码放到Text1 的change事件里
Function ss(Tmp As Long) As Boolean '素数判断
ss = True
For i = 2 To Tmp / 2
If Tmp Mod i = 0 Then
ss = False
Exit Function
End If
Next
End Function
程序如下
dim n as integer
n=text1.text
do
for i = 2 to n-1
if n mod i = 0 then exit for
next i
if i=n then text2.text = n :exit do else n=n+1
loop