VB求输入任意一个正整数,若该数不是一个素数,则找出一个大于它的最小素数.

2024-11-21 21:15:24
推荐回答(2个)
回答1:

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

回答2:

程序如下
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