放置一个command1,一个text1,一个label1,使用它们默认的名字,控件的位置和大小不用设定,程序会自动设定。代码如下:
Private Sub Command1_Click()
Dim N As Long, I As Long, J As Long, K As Long
If IsNumeric(Text1.Text) Then
N = CLng(Text1.Text)
If N Mod 1 = 0 And N >= 9 Then
For I = 3 To N \ 2
If Pd(I) Then
For J = 3 To N - I
If Pd(J) Then
K = N - I - J
If Pd(K) Then
Label1.Caption = "结果: " & N & "=" & I & "+" & J & "+" & K & "符合!"
Exit Sub
End If
End If
Next
End If
Next
Label1.Caption = "结果: 不符合!"
Else
Text1.Text = "请输入大于或等于9的奇数!"
End If
Else
Text1.Text = "请输入数字!"
End If
End Sub
Private Sub Form_Click()
Cls
Print "双击退出"
Command1.Visible = True
Text1.Visible = True
Label1.Visible = True
End Sub
Private Sub Form_DblClick()
End
End Sub
Private Sub Form_Load()
Form1.Caption = "哥德巴赫猜想"
Form1.Width = 5000
Form1.Height = 5000
Form1.AutoRedraw = True
Text1.Text = "在此输入一个大于等于9的奇数!"
Text1.ForeColor = &H808080
Text1.Top = 500
Text1.Left = 200
Text1.Width = 3000
Text1.Height = 300
Text1.Visible = False
Label1.Caption = ""
Label1.Top = 1000
Label1.Left = 200
Label1.Width = 3000
Label1.Height = 300
Label1.Visible = False
Command1.Caption = "验证"
Command1.Top = 2000
Command1.Left = 200
Command1.Width = 800
Command1.Height = 300
Command1.Visible = False
Print "1942年德国数学家哥德巴赫给数学家欧拉的一封信中,"
Print "提出了把一个整数表示成两个素数之和的推测,"
Print "也描述为:每个不小于9的奇数都可以表示为三个奇素数之和。 "
Print
Print "本Visual Basic程序用来验证这一猜想,让我们开始吧!"
Print
Print "单击开始......"
Form1.Show
End Sub
Private Sub Text1_GotFocus()
Text1.Text = ""
Text1.ForeColor = &H0
Command1.Visible = True
End Sub
Function Pd(a As Long) As Boolean
Dim b As Long
For b = 2 To a - 1
DoEvents
If a Mod b = 0 Then
Pd = False
Exit For
Else
Pd = True
End If
Next
End Function
'已经运行过.
'在窗体上添加一个command1
Private Function Ss(B As Integer) As Boolean
Dim I As Integer
Dim k As Integer
k = Sqr(B)
Ss = False
If B = 2 Or B = 3 Then Ss = True: Exit Function
For I = 2 To k
If B Mod I = 0 Then
Ss = False
Exit Function
End If
Next I
Ss = True
End Function
Private Sub Command1_Click()
Dim I As Integer
Dim A As Integer
A = Val(Text1)
For I = 2 To A / 2
If Ss(I) And Ss(A - I) Then
Text2 = A & "=" & I & "+" & A - I
Exit Sub
End If
Next I
End Sub