calculate prime number/de: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: calculate prime number/de
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Zurück zur Seite [[code examples/de|Code Beispiele]]. | |
| 2 | + | ||
| 3 | + | Dieses Beispiel enthält zwei Unterprogramme. Eines prüft, ob es sich bei der übergebenen Zahl um eine Primzahl handelt. | |
| 4 | + | ||
| 5 | + | 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. | |
| 6 | + | ||
| 7 | + | 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. | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | uses | |
| 11 | + | SysUtils, ...; | |
| 12 | + | ||
| 13 | + | ... | |
| 14 | + | ||
| 15 | + | implementation | |
| 16 | + | ||
| 17 | + | ... | |
| 18 | + | ||
| 19 | + | // Diese Funktion prüft, ob eine Zahl eine Primzahl ist | |
| 20 | + | function funIsPrim(qwdGanzzahl: QWord): boolean; inline; | |
| 21 | + | var | |
| 22 | + | qwdI: QWord; | |
| 23 | + | ||
| 24 | + | begin | |
| 25 | + | ||
| 26 | + | Result := False; | |
| 27 | + | ||
| 28 | + | for qwdI := 2 to Round(Sqrt(qwdGanzzahl)) do | |
| 29 | + | if qwdGanzzahl mod qwdI = 0 then | |
| 30 | + | exit; | |
| 31 | + | ||
| 32 | + | Result := True; | |
| 33 | + | ||
| 34 | + | end; | |
| 35 | + | ||
| 36 | + | // Diese Funktion ermittelt alle Primzahlen aus einem bestimmten Bereich | |
| 37 | + | procedure subPrimArray(qwdVon, qwdBis: QWord); | |
| 38 | + | var | |
| 39 | + | qwdI: QWord; | |
| 40 | + | arrPrim: array of Qword; | |
| 41 | + | ||
| 42 | + | begin | |
| 43 | + | ||
| 44 | + | // Legt die Anfangsgrösse des Arrays fest | |
| 45 | + | SetLength(arrPrim, 0); | |
| 46 | + | ||
| 47 | + | for qwdI := qwdVon to qwdBis do | |
| 48 | + | begin | |
| 49 | + | if funIsPrim(qwdI) = True then | |
| 50 | + | begin | |
| 51 | + | // ändert die Grösse des Arrays | |
| 52 | + | SetLength(arrPrim, High(arrPrim) + 2); | |
| 53 | + | arrPrim[High(arrPrim)] := qwdI; | |
| 54 | + | end; | |
| 55 | + | end; | |
| 56 | + | ||
| 57 | + | end; | |
| 58 | + | ||
| 59 | + | ... | |
| 60 | + | ``` | |
| 61 | + | ||
| 62 | + | --- | |
| 63 | + | *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](/wiki-contact).* |
