Sign in Join
Wiki copytemporary

calculate prime number/de

Zurück zur Seite Code Beispiele.

Dieses Beispiel enthält zwei Unterprogramme. Eines prüft, ob es sich bei der übergebenen Zahl um eine Primzahl handelt.

Das andere Unterprogramm ermittelt mit Hilfe des ersten Unterprogramms ein Array mit Primzahlen, dessen Start- und Endezahl der Prozedur nach Belieben übergeben werden können.

Die Funktionen laufen nur unter 64 Bit. Damit die Funktionen auch unter 32 Bit laufen, muss der Datentyp QWord durch den Datentyp Cardinal ersetzt werden.

pascal
uses
  SysUtils, ...;
  
  ...
  
implementation

  ...

// Diese Funktion prüft, ob eine Zahl eine Primzahl ist
function funIsPrim(qwdGanzzahl: QWord): boolean; inline;
var
  qwdI: QWord;

begin

  Result := False;

  for qwdI := 2 to Round(Sqrt(qwdGanzzahl)) do
    if qwdGanzzahl mod qwdI = 0 then
      exit;

  Result := True;

end;

// Diese Funktion ermittelt alle Primzahlen aus einem bestimmten Bereich
procedure subPrimArray(qwdVon, qwdBis: QWord);
var
  qwdI: QWord;
  arrPrim: array of Qword;

begin

  // Legt die Anfangsgrösse des Arrays fest
  SetLength(arrPrim, 0);

  for qwdI := qwdVon to qwdBis do
  begin
    if funIsPrim(qwdI) = True then
    begin
      // ändert die Grösse des Arrays
      SetLength(arrPrim, High(arrPrim) + 2);
      arrPrim[High(arrPrim)] := qwdI;
    end;
  end;

end;

  ...

From the Free Pascal wiki, page "calculate prime number/de", as it stood on 2020-02-10 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 4 people, 2013–2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
OlafCreated page
Olaf (minor)
Olaf (minor)
Olaf (minor)
Billyraybones (minor)
Olaf (minor)
Olaf (minor)
Olaf (minor)
FTurtle
TrevFixed syntax highlighting; deleted category already in page template (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.