469,612 Members | 1,673 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

The Sieve of Eratosthenes in Delphi Pascal 5

Hello,

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
  1. type
  2.   TForm1 = class(TForm)
  3.     Berechnen: TButton;
  4.     Label1: TLabel;
  5.     Edit1: TEdit;
  6.     Label2: TLabel;
  7.     Memo1: TMemo;
  8.     procedure BerechnenClick(Sender: TObject);
  9.   private
  10.     { Private-Deklarationen }
  11.   public
  12.     { Public-Deklarationen }
  13.   end;
  14.  
  15. var
  16.   Form1: TForm1;
  17.  
  18. implementation
  19.  
  20. {$R *.DFM}
  21.  
  22.  
  23.  
  24.  
  25. procedure TForm1.BerechnenClick(Sender: TObject);
  26. const
  27. n = 200;
  28. var
  29.    zahl : array [2..n] of boolean;
  30.    p,i,v : integer;
  31.    weiter : boolean;
  32. begin
  33.    for i := 2 to n do zahl[i] := true;
  34.    p := 2;
  35.    while p <= n do
  36.    begin
  37.         v := 2*p;
  38.         while v <= n do
  39.         begin
  40.              zahl[v] := false;
  41.              v := v+p;
  42.         end;
  43.         p := p+1;
  44.         if p <= n then weiter := true;
  45.         while weiter do
  46.          if (p<=n) and (zahl[p] = false)
  47.          then
  48.            p := p+1
  49.          else
  50.            weiter := false;
  51.            end;
  52.         for i := 2 to n do
  53.         if zahl[i] then
  54.         memo1.Lines.Add(IntToStr(i))
  55.            end;
  56.  
  57.  
  58. end.
2 Weeks Ago #1
0 3339

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

20 posts views Thread by MSiegel | last post: by
reply views Thread by Mark A. Washburn | last post: by
3 posts views Thread by Thys Meintjes | last post: by
15 posts views Thread by windozbloz | last post: by
28 posts views Thread by Skybuck Flying | last post: by
3 posts views Thread by genc ymeri | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.