delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


Prime numbersComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
01/21/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet prime, number, math
Times Scored:
4
Visits:
2960
Uploader: Upportune Malters
Company:
Reference: N/A
Component Download: http://www.delphi3000.com/article/2989/Prime Numbers.rar
 
Question/Problem/Abstract:
Finding out if a given number is prime
Answer:



There are some ways of finding if given number (N) is prime.
Start a loop from 2 to Sqrt(N) and if it divides with no remainder then it is not prime. Here is a source code to a simple function:

function IsPrime(N: Integer): Boolean;
var
  A: Integer;
begin
  for A := 2 to Trunc(Sqrt(N)) do
    if (N mod A) = 0 then
    begin
      Result := False;
      Exit;
    end;
  Result := True;
end;

We can see that the larger the value of N is, the longer it takes to determine if it is prime number. But what if we have to find all the prime numbers from 1 to one million, or even more? This function will be fast for the first checks but will become slower later.
Fortunately there is better solution and it is called ‘Eratostene sieve’. The idea is simple: we have a Boolean. We start with the number 2 and mark every second element as not prime (it can be divided by 2). Then continue with 3 (all that can be divided by 3 became False) and so on. And in the end (it doesen’t even matter… just joking, Hi guys from Linkin Park :) we have an array with True values indicating which number is prime.
Here is source code of the procedure I have written:

var
  NotPrime: array of Boolean;

procedure GeneratePrimes(MaxNum: Integer);
var
  A, B: Integer;
begin
  SetLength(NotPrime, MaxNum - 1); // we don't need to check the 1
  for A := 2 to MaxNum div 2 do
    if not NotPrime[A - 2] then
    begin
      B := A + A; // start from the second element
      while B <= MaxNum do
      begin
        NotPrime[B - 2] := True;
        Inc(B, A);
      end;
    end;
end;

I’ve used NotPrime and not IsPrime for name, and I use False to indicate it is prime, so I can benefit from the fact that a global array is already initialized with False values.

See the attached file to see an application written with this function.
GeneratePrimes is actually pretty fast. It’s speed depends on the amout of RAM you have on your PC. It can find the first 100 million primes for about 3 secs!!!

There are some improvements that can be made to these functions to make them faster. I wanted to demonstrate the algorithm itself and not to do the faster algorithm possible.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Don't need to check A from 2 to MaxNum div 2
    Pedro Moutinho (Mar 12 2002 6:48PM)

You only neet to check
for A := 2 to MaxNum div round(sqrt(MaxNum)) do
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
I. Siticov
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)