I need to programm the Sieve of Eratosthenes in Delphi 5 as a school homework project. This is what i currently have:
There is one problem:
At the end of the last for-"loop" it says"if zahl[i] then write(i:8);"
.
I suppose it should give out the content but what I need is the following:
It adding the number in the array, if the content of the array is yes, to an edit.
Can anyone help?
var
Form1: TForm1;
iValue, iCode: Integer;
index, anzahlgaeste: Integer;
gaeste: array of string;
zahl:array[1..1000] of boolean;
i,j,grenze:integer;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if n.text = '' then
begin
error.text := 'Feld ist leer';
n.SetFocus;
end
else begin
val(n.text, iValue, iCode);
if iCode = 0 then
begin
if StrToInt(n.text) < 2 then
begin
error.text := 'Zahl ist kleiner als zwei';
n.SetFocus;
end
else
begin
error.text := 'Erfolgreich';
//SetLength(gaeste, StrToInt(n.text));
//result.Caption := IntToStr(High(gaeste));
//for index := 0 to StrToInt(n.text) do
// begin
// result.text := result.text + 'gaeste[index]';
// end;
//https://mathematikalpha.de/primzahlsieb-des- eratosthenes
//result.text := IntToStr(sizeof(zahl));
grenze:=1000;
fillchar(zahl,sizeof(zahl),true);
i:=2; //erste Streichzahl
repeat
j:=i+i;
repeat
zahl[j]:=false;
j:=j+i; //nächste zu streichende Zahl
until j>grenze;
inc(i);
while zahl[i]=false do inc(i);
until i>sqrt(grenze);
for i:=2 to grenze do
//if zahl[i] then write(i:8);
//BoolToStr(Value: Boolean): String;
//result.text := BoolToStr(zahl[i]);
end;
end
else
begin
error.text := 'Keine (natürliche) Zahl';
n.SetFocus;
end
end;
end;
end.