First of all, I am not an native English speaker so I apologize if there are any mistakes.
I am very new to programming, especially in Delphi Pascal. Now I have to do the Sieve of Eratosthenes as school-homework. I am in the process of learning the basics so please forgive me, if I´m just struggling with a simple problem.
The code I wrote is able to list all prim numbers from 2 to 200, but how do I change it to being able to list all prim numbers of the number I type into the edit? Also is there a way to make a text show up, if there is an error?
Help would be very appreciated.
Expand|Select|Wrap|Line Numbers
- type
- TForm1 = class(TForm)
- Berechnen: TButton;
- Label1: TLabel;
- Edit1: TEdit;
- Label2: TLabel;
- Memo1: TMemo;
- procedure BerechnenClick(Sender: TObject);
- private
- { Private-Deklarationen }
- public
- { Public-Deklarationen }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.DFM}
- procedure TForm1.BerechnenClick(Sender: TObject);
- const
- n = 200;
- var
- zahl : array [2..n] of boolean;
- p,i,v : integer;
- weiter : boolean;
- begin
- for i := 2 to n do zahl[i] := true;
- p := 2;
- while p <= n do
- begin
- v := 2*p;
- while v <= n do
- begin
- zahl[v] := false;
- v := v+p;
- end;
- p := p+1;
- if p <= n then weiter := true;
- while weiter do
- if (p<=n) and (zahl[p] = false)
- then
- p := p+1
- else
- weiter := false;
- end;
- for i := 2 to n do
- if zahl[i] then
- memo1.Lines.Add(IntToStr(i))
- end;
- end.