unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 =
class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
m: Int64;
Form1: TForm1;
implementation
{$R *.dfm}
function pruefen(n: Int64): Boolean;
var
x: Int64;
p: Boolean;
begin
p := true;
x := round(n / 2);
if n >= 2
then
begin
while x > 1
do
begin
if (n
mod x) = 0
then
begin
p := false;
break;
end;
x := x - 1;
end;
end else
p := false;
if p
then // überprüfe niemals auf "= true" !!
pruefen := true
else // Willst du die Gründe wissen -> Suchfunktion.
pruefen := false;
// 'p := pruefen' müsste hier auch gehen
end;
procedure zerlegen(n: Int64);
var
x: Int64;
begin
x := 2;
while (n
mod x <> 0)
do
x := x + 1;
if Length(Form1.Label2.Caption) > 0
then
Form1.Label2.Caption := Form1.Label2.Caption + '
*';
Form1.Label2.Caption := Form1.Label2.Caption + IntToStr(x);
if pruefen(n
div x) = false
then
zerlegen(n
div x);
m := m * x;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
m := 1;
Label2.Caption := '
';
if pruefen(StrToInt(Edit1.Text))
then // kein "= true"!
begin
Label2.Caption := Edit1.Text + '
ist eine Primzahl!'
else
begin
zerlegen(StrToInt(Edit1.Text));
Label2.Caption := Label2.Caption + '
*' +
IntToStr(StrToInt(Edit1.Text)
div m);
end;
end;
end;
end.