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

2024-11-22 06:49:36
推荐回答(3个)
回答(1):

private sub command1_click()
dim i,j,n
n=val(inputbox("n="))
if IsPrime(n) then
print n & "是素数."
else
for i=n+1 to n+100
if IsPrime(i) then print "大于" & n & "的最小素数是:" & i : Exit For
next
end if
end sub

Function IsPrime(ByVal n As Long) As Boolean
Dim i As Long
if n<2 then
IsPrime = False
else
IsPrime = True
For i = 2 To sqr(n)
If n Mod i = 0 Then IsPrime = False: Exit For
Next
end if
End Function

回答(2):

Private Sub command1_click()
Dim a As Long, b As Long, n As Long

n = InputBox("请输入一个整数 N ")

If sushupd(n) Then
Print n & " 是素数 "
Else
i = n + 1
Do While Not sushupd(i)
i = i + 1
Loop
Print "大于 " & n & " 的最小素数是:" & i
End If
End Sub

Function sushupd(ByVal n As Long) As Boolean
Dim i As Long
If n < 2 Then
sushupd = False
Else
sushupd = True
i = 2
Do While i <= Sqr(n) And sushupd
If n Mod i = 0 Then sushupd = False
i = i + 1
Loop
End If
End Function

回答(3):

123