Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
490 Users Online NOW
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







Startblatt.de






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 (4)


Prime Number Class (Fast Access to Prime Numbers)Format this article printer-friendly!Bookmark function is only available for registered users!
TPrimeNumbers
Product:
Delphi 5.x (or higher)
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
02/04/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet "Prime Numbers" TPrimeNumbers Classes
Times Scored:
3
Visits:
4481
Uploader: Mike Heydon
Company: EOH
Reference: mheydon@eoh.co.za
 
Question/Problem/Abstract:
In a recent article by Upportune Malters (ID 2989) he presents an algorythm that populates a boolean array that denotes if a number is prime or not. This is an efficient idea in that once the array is generated then checking of a prime numbers is a simple check to the array element.

This prompted me to write a Class that uses Borland's TBit class. The TBit class is more memory efficient than an array of boolean in that 8 true/false values can be stored in a byte, whereas the boolean array can only store 1 true/false value in a byte.

Using the class one can check if a number is prime, obtain a list of all prime numbers from x to y.

eg.

var PN : TPrimeNumbers;

formshow or create
  PN := TPrimeNumbers.Create(1000000);  // Specify max number
end;

some routine
   if PN.IsPrime(2341) then ...

   for i := 1 to 1000 do if PN.IsPrime(i) then
      ListBox1.Add(IntToStr(i));

   etc...
end;

closeform
   PN.Free;
end;
Answer:



interface

type  

  TPrimeNumbers = class(TObject)
  private
    NotPrimeArray : TBits;    // Borland Bit array class
    FMaxNumber : integer;
  public
    constructor Create(MaxNumber : integer);
    destructor Destroy; override;
    function IsPrime(NumToCheck : integer) : boolean;
  end;

// -----------------------------------------------------------------------------

interface

constructor TPrimeNumbers.Create(MaxNumber : integer);
var A,B : integer;
begin
  NotPrimeArray := TBits.Create;          // False = PRIME
  NotPrimeArray.Size := MaxNumber + 1;    
  FMaxNumber := MaxNumber;

  for A := 2 to FMaxNumber div 2 do
    if not NotPrimeArray[A] then begin
      B := A + A;                        // Start from 2nd element
      while B <= FMaxNumber do begin
        NotPrimeArray[B] := true;        // Set NOT PRIME
        inc(B,A);
      end;
    end;

   NotPrimeArray[1] := true;             // Set elements 1 and 2
   NotPrimeArray[2] := false;
end;


destructor TPrimeNumbers.Destroy;
begin
  NotPrimeArray.Free;
end;


function TPrimeNumbers.IsPrime(NumToCheck : integer) : boolean;
begin
  if (NumToCheck < 1) or (NumToCheck > FMaxNumber) then
    Raise Exception.Create('Invalid Prime Range ' + IntToStr(NumToCheck))
  else
    Result := not NotPrimeArray[NumToCheck];
end;

end.






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Let me give you another idea
    Upportune Malters (Jan 18 2002 2:04PM)

My intentions was just to show the algorithms so I did not optimize it to keep the code clear. Since you started doing an optimization to the code let me tell you another imrovement. You can store only the odd numbers, the even are not prime. That can halve the memory used by your class.
Respond

RE: Let me give you another idea
Flurin Honegger (Feb 1 2002 7:07PM)

2 is even and prime !
Respond

RE: RE: Let me give you another idea
Mike Heydon (Feb 4 2002 8:02AM)

Quite right ... stung by the CUT&PASTE beastie again.
(Now rectified)
Respond

RE: RE: RE: Let me give you another idea
Anniel Santos (Nov 15 2005 4:42AM)

A number is Prime only if pass this.

num mod 2
Num mod 3
Num mod 5
Num mod 7

and the reminder of the division is diferent of 0. In other words a number is prime if is not divisible for 2, 3, 5, 7.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







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