הנפה של ארטוסתנס – הבדלי גרסאות

תוכן שנמחק תוכן שנוסף
Segevrl (שיחה | תרומות)
←‏קוד לנפת ארטוסתנס בשפת פייתון: כמו שכתוב בערך, מספיק לרוץ עד השורש
שורה 42:
==פסאודו קוד של נפת ארטוסתנס==
<syntaxhighlight lang="text">
algorithm Sieve of Eratosthenes is
SmallPrimeList
input: an integer n > 1.
output: all prime numbers from 2 through n.
 
let A be an array of Boolean values, indexed by integers 2 to n,
Input: n (whole number)
Output: P[] (a list ofinitially all primesset <=to n)true.
for i = 2, 3, 4, ..., not exceeding √n do
Ifif bA[i] ==is 1 thentrue
for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n do
b A[j] := 0.false
 
return all i such that A[i] is true.
1. b[n] := {1,1,...,1}. (a bit array of n one's, 1-based).
 
2. For i = 2 while i * i <= n step 1 do:
If b[i] == 1 then
For j = i * i to n step i do:
b[j] := 0.
 
3. For k = 2 to n do:
If b[k] == 1 then
add k to P[].
 
4. Return P[].
</syntaxhighlight>