早教吧 育儿知识 作业答案 考试题库 百科 知识分享

编程序证明哥德巴赫猜想.即,一个大于等于6的偶数可以表示为两个素数之和.如6=3+38=3+510=3+7这段代码怎么写,要写在formclick里面,

题目详情
编程序证明哥德巴赫猜想.即,一个大于等于6的偶数可以表示为两个素数之和.如6=3+3 8=3+5 10=3+7
这段代码怎么写,要写在form_click里面,
▼优质解答
答案和解析
Private Sub Form_Click()
Dim m As Long,a As Long,b As Long
m = Val(InputBox("请输入一个大于等于6的数","哥德巴赫猜想"))
If m Mod 2 = 0 Then
For a = 2 To m \ 2
If Prime(a) = True And Prime(m - a) = True Then Print m & " = " & a & " + " & m - a:Exit Sub
Next a
Else
For a = 2 To m \ 3
For b = a To m
If Prime(a) = True And Prime(b) = True And Prime(m - a - b) = True Then Print m & " = " & a & " + " & b & " + " & m - a - b:Exit Sub
Next b
Next a
End If
End Sub
Function Prime(ByVal n As Long) As Boolean
If n