vb 求10个100以内的素数

是随机生成的
2024-11-25 01:28:58
推荐回答(4个)
回答1:

Function isPrime(ByVal x As Integer) As Boolean
Dim si As Boolean
si = True
For i = 2 To Sqrt(x)
If x Mod i = 0 Then
si = False
Exit For
End If
Next i
isPrime = si
End Function
Sub Main()'可以是其它过程
Dim iArray(10) As Integer
Dim answer As String
Dim i As Integer
Dim itmp As Integer
Dim iIndex As Integer
answer = ""
i = 1
Do
Randomize()
itmp = Int(99 * Rnd() + 2) ‘认为1不是素数,如果你认为是素数,把这里的2改成1
For iIndex = 1 To i
If iArray(iIndex - 1) = itmp Then Exit For
Next
If isPrime(itmp) And iIndex = i + 1 Then '如果iIndexiArray(i - 1) = itmp
answer = answer & "第" & i & "个素数是:" & iArray(i - 1) & vbCrLf
i = i + 1
End If
Loop While i <= 10
MsgBox(answer)
End Sub

一组答案:

第1个素数是:7
第2个素数是:79
第3个素数是:43
第4个素数是:89
第5个素数是:31
第6个素数是:5
第7个素数是:41
第8个素数是:3
第9个素数是:29
第10个素数是:97

回答2:

for K=0 to 10
dim radnum=int(100*rnd)+1 '100内随机数
for I =0 to cint(sql(radnum))
if radnum mod I=0 then exit for
print radnum
next i
next K

回答3:

窗体上放一个按纽,结果在窗体上输出,代码如下(直接拷贝)
Private Sub Command1_Click()
Dim a(9) As Integer, n As Integer, x As Integer, j As Integer, k As Integer
n = 0
k = 1
While n < 10
k = 1
Randomize
x = Int(Rnd * 99 + 2)
For j = 2 To x - 1
If x Mod j = 0 Then k = 0
Next j
If k <> 0 Then a(n) = x: Print a(n): n = n + 1:
Wend
End Sub

回答4:

代码如下:
Private Sub Command1_Click()
Dim a(1 To 10) As Integer
Dim i%, j%, k%, n%
k = 1

For i = 100 To 1 Step -1
n = 0
For j = 1 To 100
If i Mod j = 0 Then
n = n + 1
End If
Next j
If n = 2 And k <= 10 Then
a(k) = i
k = k + 1
End If
Next i
For m = 1 To 10
Print a(m)
Next
End Sub