VB语言验证哥德巴赫猜想

来源:百度知道 编辑:UC知道 时间:2024/06/24 18:01:33
用简单程序在有限范围内验证哥德巴赫猜想

Dim a() As Long
Dim N&
Dim pstr As String

Private Sub Prime(num&)
Dim i&, j&
If num < a(0) Then Exit Sub
For j = a(0) To num Step 2
For i = 2 To N
If j Mod a(i) = 0 Then Exit For
If j < a(i) * a(i) Then
N = N + 1
ReDim Preserve a(N)
a(N) = j
pstr = pstr & "," & a(N)
Exit For
End If
Next i
Next j
a(0) = (num \ 2) * 2 + 1
End Sub

Private Sub Form_Click()
Dim jian&, jia&, k&, i&
k = InputBox("输入不小于6的偶数", , 6)
If Not IsNumeric(k) Then Exit Sub
If k <> Int(k) Then Exit Sub
If k Mod 2 Then Exit Sub
Call Prime(k) '计算较大数字是开始会比较慢,越大越慢,多用几次就快了
jia = k \ 2
Do
For i = 1 To N
If a(i) > jia Then
If InStr(pstr, k - a(i - 1)) Then
i = i - 1
Exit Do
End If
Else
jian = k - a(i - 1)
End If
Next i
For i =