急求:VB编程验证歌德巴赫猜想.即一个大于等于6的偶数可以表示为两个素数之和.
问题描述:
急求:VB编程验证歌德巴赫猜想.即一个大于等于6的偶数可以表示为两个素数之和.
答
Private Sub Command1_Click()
x = InputBox("请输入任意大于6的偶数","输入")
If Not IsNumeric(x) Then MsgBox "输入错误":Exit Sub
If x Mod 2 0 Or x Dim i As Integer,z As Integer
For i = 1 To x \ 2
z = x - i
If IsPrime(i) And IsPrime(z) Then Print x & "=" & i & "+" & z
Next
End Sub
Private Function IsPrime(n As Integer) As Boolean '判断一个数是否是素数
IsPrime = True
For j = 2 To n - 1
If n Mod j = 0 Then IsPrime = False:Exit For
Next
End Function