Delphi用代碼實現哥德巴赫猜想演示,是一個重要的數學問題,用Delphi來演示實現,後附運行效果,代碼如下示:
01
unit
Unit1;
02
interface
03
uses
04
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
05
Dialogs, StdCtrls;
06
type
07
TForm1 =
class
(TForm)
08
Edit1: TEdit;
09
Button1: TButton;
10
Edit2: TEdit;
11
procedure
Button1Click(Sender: TObject);
12
private
13
{ Private declarations }
14
public
15
{ Public declarations }
16
end
;
17
var
18
Form1: TForm1;
19
implementation
20
{$R *.dfm}
21
procedure
TForm1
.
Button1Click(Sender: TObject);
22
var
23
a,b,c,e,g :
Integer
;
24
d :
boolean
;
25
begin
26
try
27
StrToInt(Edit1
.
Text);
28
except
29
showmessage(
'必需輸入整數'
);
30
exit;
31
end
;
32
a := StrToInt(Edit1
.
Text);
33
if
((a
Mod
2
) =
0
)
and
(a >=
6
)
then
34
begin
35
for
b:=
2
to
a
do
36
begin
37
d :=
False
;
38
for
c := b -
1
downto
2
do
39
begin
40
if
(b
mod
c) =
0
then
41
break
42
else
43
begin
44
e := a - b;
45
for
g := e -
1
downto
2
do
46
begin
47
if
e
mod
g =
0
then
48
begin
49
d :=
False
;
50
break;
51
end
;
52
d :=
True
;
53
end
;
54
end
;
55
if
d=
True
then
break;
56
end
;
57
if
d=
True
then
58
begin
59
Edit2
.
Text :=
'結果:符合!'
+
' '
+IntToStr(a)+
'='
+IntToStr(e)+
'+'
+IntToStr(b);
60
break;
61
end
62
end
;
63
end
64
else
65
Edit2
.
Text :=
'結果:不符合'
;
66
end
;
67
end
.
Delphi演示的哥德巴赫猜想運行效果: