473,414 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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.
Nov 14 '21 #1
0 3928

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

Similar topics

20
by: MSiegel | last post by:
hi there! i have to program the sieve of eratosthenes in php as a homework. after i had created an html file where the maximum is set i wrote a php script which doesn't work properly - actually...
0
by: Mark A. Washburn | last post by:
/* SIEVE OF ERATOSTHENES from BYTE magazine -------------------- -- compiled with jdk 1.1.7b with optimize on ( -O) -- Run times on 300 MHz Pentium 2 Windows 95 -- in order of output, from...
3
by: Thys Meintjes | last post by:
Greets, I have need of a Delphi/pascal to python converter. Googling didn't suggest any obvious leads so I'm trying here... Thanks Thys
15
by: windozbloz | last post by:
Bye Bye Billy Bob... I'm back with one more question, then I'll chill. I have scoured the news and net for info about Borlands KYLIX 3 and have found little technical info about it. Their...
28
by: Skybuck Flying | last post by:
Hi, I think I understand now a bit better what the difference is between a c compiler and a pascal compiler. For example: When compiling source code with a pascal compiler. The pascal...
3
by: genc ymeri | last post by:
Hi, What can I use in C# for dynamic arrays ???? I have some records (struts in ..Net) and want to store them in a dynamic "arrays" or object list. I noticed the in C# arrays' length can't be...
4
by: knuxus | last post by:
Hey everyone.... I would appreciate any type of help if anyone could explain me how to translate this algorithm to Visual Basic, im still learning and i would appreciate this algorithm to my prime...
2
Blackout
by: Blackout | last post by:
Hi, I'm having problems with this C program. Whenever I run it, it doesn't print anything. The program is supposed to compute and display all the prime numbers from 1 - 300 using the sieve of...
0
by: Pheddy | last post by:
Hi all. Im having some trouble with some Pascal coding and have tried my usual forum (delphipages.com) But since they sometimes are pretty slow to respond, I would like to ask if theres any Delphi...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.