VB素数高手请进。
来源:百度知道 编辑:UC知道 时间:2024/06/21 22:48:22
不给分啊…大概说个思路:1、100个数里挑50个的话可以直接随机产生一个,直到不与之前的重复(int(rnd()*101)+200)。2、判断素数即for i=2 to sqr(n),if n/i=int(n/i) then 不是素数(其中n是要检测的数)。3、排序方法网上都有
Sub 指定范围随机素数()
Dim 随机整数(49) As Long
Dim 随机素数() As Long, 素数_Index As Long
Dim 临时数 As Long
Dim 有重复 As Boolean
Dim i As Long, i2 As Long
素数_Index = -1
For i = 0 To 49
'提取未出来过的临时数
Randomize
Select Case i
Case 0
随机整数(i) = 200 + Int(Rnd * 101)
Case Else
Do
临时数 = 200 + Int(Rnd * 101)
有重复 = False
For i2 = 0 To i
If 随机整数(i2) = 临时数 Then
有重复 = True
Exit For
End If
Next i2
If 有重复 = False Then Exit Do
Loop
随机整数(i) = 临时数
End Select
'判断该随机整数是否质数,能整除2、3、5、7的数都不是质数
If 随机整数(i) Mod