|

楼主 |
发表于 2021-10-18 10:33
|
显示全部楼层
如下程序速度非常快,是高手做的程序,VB编程论坛高手回复了你的帖子。
程序运行结果:4457973个数无法表示为p+2^N的形式,用时20.8479999999981秒.
Msgbox上显示的是19.6秒,1亿内的奇数算完了,够快吧!
如下是代码:
Dim pri(100000000) As Byte, gusnum(10) As Long
Sub getpri()
'获取一亿内素数map,5秒内可完成
Dim i As Long, j As Long
pri(0) = 1
pri(1) = 1
For i = 4 To 100000000 Step 2
pri(i) = 1
Next
For i = 3 To 10000 Step 2
If pri(i) = 0 Then
For j = i * i To 100000000 Step i * 2
pri(j) = 1
Next
End If
Next
End Sub
Private Sub Command1_Click()
'验证猜想,统计不符合的总数,并把前10个数存到数组中供验证
Dim i As Long, j As Long, k As Long, t As Double
t = Timer()
For i = 5 To 100000000 Step 2
j = 2
Do While j < i
If pri(i - j) = 0 Then Exit Do
j = j * 2
Loop
If j >= i Then
If k <= 10 Then gusnum(k) = i
k = k + 1
End If
Next
Text1 = k & "个数无法表示为p+2^N的形式,用时" & Timer() - t & "秒"
MsgBox k & "个数无法表示为p+2^N的形式,用时" & Timer() - t & "秒"
End Sub
Private Sub Form_Load()
getpri
Command1_Click
End Sub
|
|