#2
aliceabcd2017-06-16 16:11
|
验证哥德巴赫猜想:任意一个大于等于6的偶数都可以分解为两个质数之和。
基本思想:n为大于等于6的任一偶数,可分解为n1和n2两个数,
分别检查n1和n2是否为素数,如都是,则为一组解。
如n1不是素数,就不必再检查n2是否素数。先从n1=3开始,
检验n1和n2(n2=N-n1)是否素数。然后使n1+2
再检验n1、n2是否素数,… 直到n1=n/2为止。
我的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label2.Text = ""
Dim a, n, i, j, k, fir, sec As Integer, flag, fla As Boolean
a = Val(TextBox1.Text)
For n = 3 To a / 2 Step 2
flag = True
For i = 2 To (n - 1)
If n Mod i = 0 Then
flag = False : Exit For
End If
Next i
If flag Then
fir = n
sec = a - n
For k = 3 To sec / 2 Step 2
fla = True
For j = 2 To (k - 1)
If n Mod j = 0 Then
fla = False : Exit For
End If
Next j
If fla Then
Label2.Text += a.ToString + "=" + fir.ToString + "+" + sec.ToString + Chr(10)
ElseIf fla = False Then
Exit For
End If
Next k
ElseIf flag = False Then
Exit For
End If
Next n
End Sub
End Class
结果出来的不对,一样的结果出来好几遍,求大神帮改!!!