AW: Delphi errechnet keine Primzahlen größer als 5000
Und dann ist das noch so, das alle Primzahlen > 3 der Form 6n+/-1 sind. Also kann man alle Primzahlen so prüfen
Delphi-Quellcode:
Und die Funktion IsPrime prüft nicht bis Wert/2, sondern nur bis Sqrt(Wert) und da auch nur mit allen bisherigen Primzahlen, also:
// 2,3 => Primzahl
p := 5; repeat If IsPrime(P) then AddToPrimeList(P); If IsPrime(P+2) then AddToPrimeList(P+2); inc(P,6); until p>maxint-6; // oder so ähnlich
Delphi-Quellcode:
Ist aber ungetestet.
Function IsPrime (P : Integer): Boolean;
Begin MaxP := Trunc(Sqrt(P)); I := 0; While (Primes[I]<MaxP) do If P Mod Primes[I]<>0 then exit(false) else inc(I); exit(true) End; Und noch eigentlicher verwendet man 'Sieve of Atkin' oder 'Sieve of Eratosthenes'. Da bekommt man dann alle Primzahlen < 2^31 in so 1-2 Sekunden, wenn ich mich richtig erinnere. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:16 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz