题库 高中信息

题干

任意一个大于等于4 的偶数,都可以用两个素数之和表示,这就是哥德巴赫猜想。如:4=2+2,6=3+3,8=3+5,10=5+5,12=5+7,98=17+79。
编写一个验证哥德巴赫猜想的VB 程序:程序运行时,在文本框Text1 中输入一个大于等于4 的偶数,单击“验证”按钮Command1 后,如果哥德巴赫猜想验证成功,则在文本框Text2 中显示“Yes”,并在列表框List1 中显示用两个素数表示该偶数的等式,否则显示“No”。程序运行结果如图所示。实现上述功能的VB 代码如下,但加框处代码有错,请改正。

函数judge的功能是判断整数x是否为素数,若是素数则返回True,否则返回False
Function judge(x As Integer)As Boolean
Dim j As Integer
judge = True
j = 2
Do While j < =" x" - 1 And judge = True
If  x Mod j =" 0" Then
judge = False
Else
judge=true    ‘①
End If
Loop
End Function
Private Sub Command1_Click()
Dim n As Integer, p As Integer, q As Integer, flag As Boolean
n = Val(Text1.Text)
p = 1
flag = False
Do While flag = True And p < n ‘②
p =" p" + 1
q =" q" + 1     ‘
If judge(p)  And  judge(q) Then
Text2.Text = "Yes"
flag = True
List1.AddItem Str(n)+ "=" + Str(p)+ "+" + Str(q)
End If
Loop
If flag ="False" Then Text2.Text = "No"
End Sub
上一题 下一题 0.99难度 填空题 更新时间:2017-02-24 09:45:38

答案(点此获取答案解析)