Sieve of Pritchard: Difference between revisions

Added Easylang
(Added Easylang)
 
(27 intermediate revisions by 13 users not shown)
Line 2:
[[Category:Simple]]
 
The [[wp:Sieve_of_Pritchard|Sieve of Pritchard]] is a modernan algorithm for finding the prime numbers up to a given limit N, published in 1981. It takesconsiders many fewer operationscomposite numbers than the [[Sieve of Eratosthenes|Sieve of Eratosthenes]] (and has a better asymptotic time complexity). However, atunlike the costlatter, ofit greatercannot storagebe requirementsmodified (worseto greatly reduce its space complexity)requirement, making it unsuitable for very large limits.
 
Conceptually, it works by constructingbuilding a serieswheel ofrepresenting "wheels"the marked along their circumference with therepeating pattern of primesnumbers upnot todivisible theby valueone of successivethe primorialfirst numbersk (whereprimes, theincreasing Nthk primorial isuntil the productsquare of the firstk'th Nprime primes).is Thoseat wheelsleast areN. thenSince rolledwheels alonggrow the numbervery linequickly, andthe onlyalgorithm therestricts numbersattention touched byto the marksinitial areportions consideredof aswheels candidate primes, in contrastup to Eratosthenes'N. sieve(Small inexamples which allof the integerswheels inconstructed by the range start out as candidates. (The Sieve of Pritchard isare anused example ofin the "wheel-based optimizations" mentioned in the Eratosthenes task.)
 
For example, the second-order wheel has sizecircumference 6 (the product of the first two primes, 2 and 3) and is marked only at the numbers between 1 and 6 that are not multiples of 2 or 3, namely 1 and 5. As this wheel is rolled along the number line, it will pick up only numbers of the form 6k+1 or 6k+5 (that is, n where n mod 6 is in {1,5}). By the time it stops at 30 (2x3x5) it has added only 8 of the numbers between 6 and 30 as candidates for primality,. onlyThose onethat are multiples of which5 is(only actually2: composite1*5 and must5*5) beare removedobtained (25).by Inmultiplying the processmembers it has constructedof the nextsecond-order wheel,. whichRemoving willthem addgives onlythe ninenext outwheel, of every 30 numbers as it rolls upand toso 210on.
 
[https://www.youtube.com/watch?v=h9EHkZLekoYGxgGMwLfTjE This YouTube video] tells a story to help motivate the algorithm's design;[https://www.youtube.com/watch?v=GxgGMwLfTjE this one] presents the execution of the algorithm for N=150 in a format that permits single-stepping forward and backward through the run. In that implementation, the list of primeswheel is populatedrepresented intoby a sparse global array <tt>s</tt> such that for each member w of the wheel, <tt>s[pw]</tt> contains the next primemember afterof pthe iffwheel; palong is itselfwith a primesimilar in"previous themember" target range;value, this allows numbers to be removed fromin considerationa quicklyconstant withoutnumber anyof operations. But the copying/shiftingsimple thatabstract wouldalgorithm beis requiredbased fromon aan normally-packedordered arrayset, and there is plenty of scope for different implementations.
 
;Task:
Line 31:
if (limit < 2) then return {}
script o
property primeswheel : {2}
property wheelextension : {1,missing 2}value
property oldWheel : missing value
end script
set {oldCircumference, circumference} to {missing value, 2}
set {x, newCircumference, currentPrime, mv} to {0, 2, 1, missing value}
repeat until (oldCircumference = limit)
repeat until (currentPrime * currentPrime > limit)
set o's oldWheel to o's wheel's numbers
set-- primeGet tothe o'snext confirmed prime oldWheel'sfrom secondthe itemwheel.
set endx ofto o'sx primes to+ prime1
set oldCircumferencecurrentPrime to circumferenceo's wheel's item x
-- Get an extension list nominally expanding the wheel to the lesser of
set circumference to oldCircumference * prime
if-- (circumferenceits >current limit)circumference then* setcurrentPrime circumferenceand tothe limit parameter.
repeat-- withIt'll nbe fromfar (oldCircumferencelonger +than 1)needed, tobut circumferencehey.
if (o's wheel's item ((n - 1) modset oldCircumference + 1) is missing value)to thennewCircumference
set endnewCircumference ofto o's wheel tooldCircumference missing* valuecurrentPrime
if (newCircumference > limit) then set newCircumference to limit
else
set end of o's wheelextension to nmakeList(newCircumference - oldCircumference, mv)
-- Insert numbers that are oldCircumference added to 1 and to each number currently in the
-- unpartitioned part of the wheel, except where the results are multiples of currentPrime.
set k to 0
set listLen to (count o's wheel)
repeat with augend from oldCircumference to (newCircumference - 1) by oldCircumference
set n to augend + 1
if (n mod currentPrime > 0) then
set k to k + 1
set o's extension's item k to n
end if
repeat with i from x to listLen
set n to augend + (o's wheel's item i)
if (n > newCircumference) then exit repeat
if (n mod currentPrime > 0) then
set k to k + 1
set o's extension's item k to n
end if
end repeat
end repeat
-- Find and delete multiples of the current prime which occur in the old part of the wheel.
repeat with this in o's oldWheel
set nmaxMultiple to thisoldCircumference *div primecurrentPrime
set i to x
if (n > circumference) then exit repeat
repeat while set ((o's wheel's item n toi) missing< valuemaxMultiple)
set i to i + 1
end repeat
end repeat with i from i to x by -1
set j to binarySearch((o's wheel's item i) * currentPrime, o's wheel, i, listLen)
return o's primes & rest of o's wheel's numbersif (j > 0) then
set o's wheel's item j to mv
end sieveOfPritchard
set listLen to j - 1
 
sieveOfPritchard(150)</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149}</syntaxhighlight>
 
While the above's fine for the current task, if it were needed to return primes up to the hundreds of thousands and beyond, it would be much faster to prefabricate the 'wheel' list to its final length by means of concatenation than to grow it on the fly by appending items.
 
<syntaxhighlight lang="applescript">on sieveOfPritchard(limit)
if (limit < 2) then return {}
script o
property primes : {}
property wheel : makeList(limit, missing value)
property oldWheel : missing value
end script
set {o's wheel's 1st item, o's wheel's 2nd item} to {1, 2}
set {oldCircumference, circumference} to {missing value, 2}
repeat until (oldCircumference = limit)
set o's oldWheel to o's wheel's numbers
set prime to o's oldWheel's second item
set end of o's primes to prime
set oldCircumference to circumference
set circumference to oldCircumference * prime
if (circumference > limit) then set circumference to limit
repeat with n from (oldCircumference + 1) to circumference
if (o's wheel's item ((n - 1) mod oldCircumference + 1) is not missing value) then
set o's wheel's item n to n
end if
end repeat
-- Keep the undeleted numbers and any in the extension list.
repeat with this in o's oldWheel
set o's set nwheel to thiso's *wheel's primenumbers
if (nk > circumference0) then exitset o's wheel to o's wheel & o's extension's items 1 thru repeatk
set o's wheel's item n to missing value
end repeat
end repeat
return o's primes & rest of o's wheel's numbers
end sieveOfPritchard
 
Line 115 ⟶ 102:
end makeList
 
on binarySearch(n, theList, l, r)
sieveOfPritchard(1000000)</syntaxhighlight>
script o
property lst : theList
end script
repeat until (l = r)
set m to (l + r) div 2
if (o's lst's item m < n) then
set l to m + 1
else
set r to m
end if
end repeat
if (o's lst's item l is n) then return l
return 0
end binarySearch
 
sieveOfPritchard(150)</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149}</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">arraybase 1
call sieveOfPritchard(150, true)
call sieveOfPritchard(1e6, false)
end
 
function min(a, b)
if a < b then return a else return b
end function
 
subroutine sieveOfPritchard(limit, imprime)
dim members[limit + 1] fill false
members[1] = true
ub = members[?]
stepLength = 1
prime = 2
rtlim = sqr(limit)
nlimit = 2
dim primes[1]
cont = 0
 
while prime <= rtlim
if stepLength < limit then
for w = 1 to ub
if members[w] then
dim n = w + stepLength
while n <= nlimit
members[n] = true
n += stepLength
end while
end if
next
stepLength = nlimit
end if
 
np = 5
dim mcpy[ub]
for i = 1 to ub
mcpy[i] = members[i]
next
 
for i = 1 to ub
if mcpy[i] then
if np = 5 and i > prime then np = i
dim n = prime * i
if n > limit then exit for
members[n] = false
end if
next
 
if np < prime then exit while
cont += 1
redim primes(cont)
primes[cont] = prime
if prime = 2 then prime = 3 else prime = np
nlimit = min(stepLength * prime, limit)
end while
 
dim newPrimes(ub)
for i = 2 to ub
if members[i] then newPrimes[i] = i
next
 
cont = 0
for i = 1 to primes[?]
if imprime then print " "; primes[i];
cont += 1
next
for i = 1 to ub
if newPrimes[i] then
cont += 1
if imprime then print " "; i;
end if
next
if not imprime then print : print "Number of primes up to "; limit; ": "; cont
end subroutine</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
{{trans|Wren}}
<syntaxhighlight lang="vb">#define min(a, b) iif((a) < (b), (a), (b))
 
Sub sieveOfPritchard(limit As Uinteger, imprime As Boolean)
Dim As Boolean members(1 To limit + 1)
members(1) = True
Dim As Uinteger ub = Ubound(members)
Dim As Uinteger stepLength = 1
Dim As Uinteger prime = 2
Dim As Uinteger rtlim = Sqr(limit)
Dim As Uinteger nlimit = 2
Dim As Integer primes()
Dim As Integer i, cont = 0
While prime <= rtlim
If stepLength < limit Then
For w As Integer = 1 To ub
If members(w) Then
Dim As Integer n = w + stepLength
While n <= nlimit
members(n) = True
n += stepLength
Wend
End If
Next
stepLength = nlimit
End If
Dim As Uinteger np = 5
Dim As Boolean mcpy(ub)
For i = 1 To ub
mcpy(i) = members(i)
Next
For i = 1 To ub
If mcpy(i) Then
If np = 5 And i > prime Then np = i
Dim As Uinteger n = prime * i
If n > limit Then Exit For there.
members(n) = False
End If
Next
If np < prime Then Exit While
cont += 1
Redim Preserve primes(cont)
primes(cont) = prime
prime = Iif(prime = 2, 3, np)
nlimit = min(stepLength * prime, limit)
Wend
Dim As Integer newPrimes(ub)
For i = 2 To ub
If members(i) Then newPrimes(i) = i
Next
cont = 0
For i = 1 To Ubound(primes)
If imprime Then Print primes(i);
cont += 1
Next
For i = 1 To ub
If newPrimes(i) Then
cont += 1
If imprime Then Print i;
End If
Next
If Not imprime Then Print !"\nNumber of primes up to "; limit; ":"; cont
End Sub
 
sieveOfPritchard(150, True)
sieveOfPritchard(1e6, False)
 
Sleep</syntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149
Number of primes up to 1000000: 78498</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FUNCTION min(a, b)
IF a < b THEN LET min = a ELSE LET min = b
END FUNCTION
 
SUB sieveofpritchard (limit)
DIM members(0)
MAT REDIM members(limit+1)
LET members(1) = 1
LET ub = UBOUND(members)
LET steplength = 1
LET prime = 2
LET rtlim = SQR(limit)
LET nlimit = 2
DIM primes(10)
LET cnt = 0
DIM mcpy(0)
MAT REDIM mcpy(ub)
 
DO WHILE prime <= rtlim
IF steplength < limit THEN
FOR w = 1 TO ub
IF members(w)<>0 THEN
LET n = w+steplength
DO WHILE n <= nlimit
LET members(n) = 1
LET n = n+steplength
LOOP
END IF
NEXT w
LET steplength = nlimit
END IF
 
LET np = 5
FOR i = 1 TO ub
LET mcpy(i) = members(i)
NEXT i
 
FOR i = 1 TO ub
IF mcpy(i)<>0 THEN
IF np = 5 AND i > prime THEN LET np = i
LET n = prime*i
IF n > limit THEN EXIT FOR
LET members(n) = 0
END IF
NEXT i
 
IF np < prime THEN EXIT DO
LET cnt = cnt+1
MAT REDIM primes(cnt)
LET primes(cnt) = prime
IF prime = 2 THEN LET prime = 3 ELSE LET prime = np
LET nlimit = min(steplength*prime, limit)
LOOP
 
DIM newprimes(0)
MAT REDIM newprimes(ub)
FOR i = 2 TO ub
IF members(i)<>0 THEN LET newprimes(i) = i
NEXT i
 
LET cnt = 0
FOR i = 1 TO UBOUND(primes)
PRINT primes(i);
LET cnt = cnt+1
NEXT i
FOR i = 1 TO ub
IF newprimes(i)<>0 THEN
PRINT i;
LET cnt = cnt+1
END IF
NEXT i
END SUB
 
CLEAR
CALL sieveofpritchard (150)
END</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">sieveOfPritchard(150, true)
sieveOfPritchard(1e6, false)
end
 
sub sieveOfPritchard(limit, imprime)
dim members(limit + 1)
members(1) = true
ub = arraysize(members(),1)
stepLength = 1
prime = 2
rtlim = sqrt(limit)
nlimit = 2
dim primes(1)
cont = 0
 
while prime <= rtlim
if stepLength < limit then
for w = 1 to ub
if members(w) then
n = w + stepLength
while n <= nlimit
members(n) = true
n = n + stepLength
wend
fi
next
stepLength = nlimit
fi
 
np = 5
dim mcpy(ub)
for i = 1 to ub
mcpy(i) = members(i)
next
 
for i = 1 to ub
if mcpy(i) then
if np = 5 and i > prime np = i
n = prime * i
if n > limit break
members(n) = false
fi
next
 
if np < prime break
cont = cont + 1
redim primes(cont)
primes(cont) = prime
if prime = 2 then prime = 3 else prime = np : fi
nlimit = min(stepLength * prime, limit)
wend
 
dim newPrimes(ub)
for i = 2 to ub
if members(i) newPrimes(i) = i
next
 
cont = 0
for i = 1 to arraysize(primes(),1)
if imprime print " ", primes(i);
cont = cont + 1
next
for i = 1 to ub
if newPrimes(i) then
cont = cont + 1
if imprime print " ", i;
fi
next
if not imprime then print : print "Number of primes up to ", limit, ": ", cont : fi
end sub</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
 
=={{header|C#|CSharp}}==
Line 121 ⟶ 442:
 
Compared to the prototype algorithm, it appears there isn't any code to do the follow-up end-of-wheel additions when necessary. But the main loop limit has been changed to go to the next prime, and the existing code handles the additions.
 
Updated to include "numbers added / removed (to / from ''members'')" and performance statistics. The "removed" figure includes both composite numbers and prime numbers less than the square root of ''limit''. The Wikipedia article indicates only eight removals (for ''limit'' = 150) because it doesn't count the removed primes and the initial ''1'' that the ''members'' array is initialized with.
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
Line 127 ⟶ 450:
 
// Returns list of primes up to limit using Pritchard (wheel) sieve
static List<int> PrimesUpTo(int limit, bool verbose = false) {
var sw = System.Diagnostics.Stopwatch.StartNew();
var members = new SortedSet<int>{ 1 };
int stp = 1, prime = 2, n, nxtpr, rtlim = 1 + (int)Math.Sqrt(limit), nl, ac = 2, rc = 1;
varList<int> primes = new List<int>(), tl = new List<int>();
while (prime <= rtlim) {
nl = Math.Min(prime * stp, limit);
if (stp < limit) {
var nu = new List<int>tl.Clear();
foreach (var w in members)
for (n = w + stp; n <= nl; n += stp) nutl.Add(n);
members.UnionWith(nutl); ac += tl.Count;
}
stp = nl; // update wheel size to wheel limit
nxtpr = 05; // for obtaining the next prime
var wb = new List<int>tl.Clear();
foreach (var w in members) {
if (nxtpr == 05 && w > prime) nxtpr = w;
if (members.Contains(n = prime * w) > nl) wbbreak; else tl.Add(n);
}
foreach (var itm in wbtl) members.Remove(itm); rc += tl.Count;
primes.Add(prime);
prime = prime == 2 ? 3 : nxtpr;
nl = Math.Min(limit, prime * stp);
}
members.Remove(1); primes.AddRange(members); sw.Stop();
if (verbose) Console.WriteLine("Up to {0}, added:{1}, removed:{2}, primes counted:{3}, time:{4} ms", limit, ac, rc, primes.Count, sw.Elapsed.TotalMilliseconds);
primes.AddRange(members);
return primes;
}
 
static void Main(string[] args) {
Console.WriteLine("[{0}]", string.Join(", ", PrimesUpTo(150, true)));
PrimesUpTo(1000000, true);
}
}</syntaxhighlight>
{{out}}Timing from Tio.run:
<pre>Up to 150, added:45, removed:14, primes counted:35, time:13.2842 ms
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149]
Up to 1000000, added:186825, removed:108494, primes counted:78498, time:139.4323 ms
</pre>
 
=={{header|C++}}==
We obtain a simple but high-performance implementation.
 
The starting idea is to represent W as simply as possible, with an array w[] containing the members in order;
i.e. w[i] is the i'th member (indexing from 0).
When the current wheel is extended by rolling it, the code simply iterates through the array w, adding the length of the wheel to each member w[i]
and appending the result.
The other step is to delete the composites formed by multiplying the values in the current wheel by the current prime p.
However, this presents problems, firstly because each multiple cannot be found in w in O(1) time.
Accordingly, a bit array d[] (for "deleted") is introduced such that d[x] is initialized to false when a value x is appended to W,
and is set to true should x be deleted as a multiple p*w[i] of p.
Deletions are now fast, but the array is left containing deleted elements.
 
So if the new W will be extended in the next iteration, because its length < N, then the array w is compressed by eliminating the deleted values.
But once the length reaches N (which happens very quickly), it would be way too costly to compress w at the end of each iteration.
However, only the values in W up to N/p will be used as factors in the next lot of deletions.
So it suffices to compress only this initial section of w.
When the remaining primes are gathered on completion, it is necessary to skip zero and deleted values in w.
 
Each low-level operation in the resulting algorithm can be associated with an abstract operation so that each of the latter gets O(1) operations.
So the resulting program still runs in O(N/log log N) time, and the implicit constant factor is quite small.
 
<syntaxhighlight lang="cpp">/* Sieve of Pritchard in C++, as described at https://en.wikipedia.org/wiki/Sieve_of_Pritchard */
/* Simple but high-performance implementation using a simple array of integers and a bit array (using bytes for speed). */
/* 2 <= N <= 1000000000 */
/* (like the standard Sieve of Eratosthenes, this algorithm is not suitable for very large N due to memory requirements) */
 
#include <cstring>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctime>
 
void Extend (uint32_t w[], uint32_t &w_end, uint32_t &length, uint32_t n, bool d[], uint32_t &w_end_max) {
/* Rolls full wheel W up to n, and sets length=n */
uint32_t i, j, x;
i = 0; j = w_end;
x = length + 1; /* length+w[0] */
while (x <= n) {
w[++j] = x; /* Append x to the ordered set W */
d[x] = false;
x = length + w[++i];
}
length = n; w_end = j;
if (w_end > w_end_max) w_end_max = w_end;
}
 
void Delete (uint32_t w[], uint32_t length, uint32_t p, bool d[], uint32_t &imaxf) {
/* Deletes multiples p*w[i] of p from W, and sets imaxf to last i for deletion */
uint32_t i, x;
i = 0;
x = p; /* p*w[0]=p*1 */
while (x <= length) {
d[x] = true; /* Remove x from W; */
x = p*w[++i];
}
imaxf = i-1;
}
 
void Compress(uint32_t w[], bool d[], uint32_t to, uint32_t &w_end) {
/* Removes deleted values in w[0..to], and if to=w_end, updates w_end, otherwise pads with zeros on right */
uint32_t i, j;
j = 0;
for (i=1; i <= to; i++) {
if (!d[w[i]]) {
w[++j] = w[i];
}
}
if (to == w_end) {
w_end = j;
} else {
for (uint32_t k=j+1; k <= to; k++) w[k] = 0;
}
}
 
void Sift(uint32_t N, bool printPrimes, uint32_t &nrPrimes, uint32_t &vBound) {
/* finds the nrPrimes primes up to N, printing them if printPrimes */
uint32_t *w = new uint32_t[N/4+5];
bool *d = new bool[N+1];
uint32_t w_end, length;
/* representation invariant (for the main loop): */
/* if length < N (so W is a complete wheel), w[0..w_end] is the ordered set W; */
/* otherwise, w[0..w_end], omitting zeros and values w with d[w] true, is the ordered set W, */
/* and no values <= N/p are omitted */
uint32_t w_end_max, p, imaxf;
/* W,k,length = {1},1,2: */
w_end = 0; w[0] = 1;
w_end_max = 0;
length = 2;
/* Pr = {2}: */
nrPrimes = 1;
if (printPrimes) printf("%d", 2);
p = 3;
/* invariant: p = p_(k+1) and W = W_k inter {1,...,N} and length = min(P_k,N) and Pr = the first k primes */
/* (where p_i denotes the i'th prime, W_i denotes the i'th wheel, P_i denotes the product of the first i primes) */
while (p*p <= N) {
/* Append p to Pr: */
nrPrimes++;
if (printPrimes) printf(" %d", p);
if (length < N) {
/* Extend W with length to minimum of p*length and N: */
Extend (w, w_end, length, std::min(p*length,N), d, w_end_max);
}
Delete(w, length, p, d, imaxf);
Compress(w, d, (length < N ? w_end : imaxf), w_end);
/* p = next(W, 1): */
p = w[1];
if (p == 0) break; /* next p is after zeroed section so is too big */
/* k++ */
}
if (length < N) {
/* Extend full wheel W,length to N: */
Extend (w, w_end, length, N, d, w_end_max);
}
/* gather remaining primes: */
for (uint32_t i=1; i <= w_end; i++) {
if (w[i] == 0 || d[w[i]]) continue;
if (printPrimes) printf(" %d", w[i]);
nrPrimes++;
}
vBound = w_end_max+1;
}
 
int main (int argc, char *argw[]) {
bool error = false;
bool printPrimes = false;
uint32_t N, nrPrimes, vBound;
if (argc == 3) {
if (strcmp(argw[2], "-p") == 0) {
printPrimes = true;
argc--;
} else {
error = true;
}
}
if (argc == 2) {
N = atoi(argw[1]);
if (N < 2 || N > 1000000000) error = true;
} else {
error = true;
}
if (error) {
printf("call with: %s N -p where 2 <= N <= 1000000000 and -p to print the primes is optional \n", argw[0]);
exit(1);
}
int start_s = clock();
Sift(N, printPrimes, nrPrimes, vBound);
int stop_s=clock();
printf("\n%d primes up to %lu found in %.3f ms using array w[%d]\n", nrPrimes,
(unsigned long)N, (stop_s-start_s)*1E3/double(CLOCKS_PER_SEC), vBound);
}</syntaxhighlight>
{{out}}
<pre>[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149]</pre>
35 primes up to 150 found in 0.038 ms using array w[40]
 
50847534 primes up to 1000000000 found in 2339.566 ms using array w[163588196]</pre>
 
=={{header|EasyLang}}==
{{trans|Julia}}
<syntaxhighlight>
proc pritchard limit . primes[] .
len members[] limit
members[1] = 1
steplength = 1
prime = 2
rtlimit = sqrt limit
nlimit = 2
primes[] = [ ]
while prime <= rtlimit
if steplength < limit
for w to len members[]
if members[w] = 1
n = w + steplength
while n <= nlimit
members[n] = 1
n += steplength
.
.
.
steplength = nlimit
.
np = 5
mcpy[] = members[]
for w to nlimit
if mcpy[w] = 1
if np = 5 and w > prime
np = w
.
n = prime * w
if n > nlimit
break 1
.
members[n] = 0
.
.
if np < prime
break 1
.
primes[] &= prime
if prime = 2
prime = 3
else
prime = np
.
nlimit = lower (steplength * prime) limit
.
for i = 2 to len members[]
if members[i] = 1
primes[] &= i
.
.
.
pritchard 150 p[]
print p[]
</syntaxhighlight>
 
{{out}}
<pre>
[ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 ]
</pre>
 
=={{header|J}}==
Implementation:<syntaxhighlight lang="j">pritchard=model: {{
<syntaxhighlight lang="j">pritchard=: {{N=. y
spokes=. $.6$4{.1
primesroot=. 2,>.@%: p=.3N
spokes=. 1
while. y > #spokes do.
primes=. ''
p=. 0
while. p<:root do.
primes=. primes, p=. 2+(}.spokes) i.1 NB. find next prime
rim=. #spokes NB. "length" of "circumference" of wheel
spokes=. (yN<.p*rim)$spokes NB. roll next larger wheel
NB. remove multiples of this next prime:
spokes=. 8 $.0 ((#~ y>])_1+p*1+i.rim)} spokes NB. remove newly recognized prime from wheel
spokes=. 0 ((#spokes) (>#]) _1+p*1+i.rim)} spokes NB. remove newly recognized prime from wheel
end.
N (>:#]) primes,1+}.,I.spokes
while. y > p*p do.
primes=. primes, p=. 2+(}.spokes) i.1 NB. find next prime
spokes=. 0 ((#~ y>])_1+p*1+i.rim)} spokes NB. scrub it out of wheel
end.
primes,1+}.,I.spokes
}}</syntaxhighlight>
 
Line 182 ⟶ 732:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149</syntaxhighlight>
 
However, this approach exhibits performance problems when N is large.
This task exposed a bug in J's implementation of X i. Y when X is an empty sparse array. To avoid/work-around this bug, we initialize <code>spokes</code> and <code>primes</code> with the values that they would have had after the first two iterations of this algorithm if <code>i.</code> would have worked properly. (The bug has been fixed, but the fix is not yet widely deployed. (We could also have used a dense array instead of a sparse array, but it's probably more "in spirit with the design of this task" to "hard code" the first two rounds and use sparse arrays than it would be to use dense arrays for the wheels.))
 
A faster approach recognizes when the wheel is large enough and treats all subsequent "next primes" specially:
 
<syntaxhighlight lang="j">pr =: {{N=.y
root=. <.%:N NB. performance optimzation
circumference=. 1
spokes=. ,1
primes=. ''
while. N > L=. circumference do.
primes=. primes, p =. 1{ spokes,L+1 NB. next prime from sieve
circumference=. N <. p * L NB. next larger wheel:
spokes=. circumference (>:#]), spokes +/~ L * i.circumference >.@% L
NB. remove multiples of this next prime:
spokes=. spokes -. p * spokes ( [{.~ >:@:(I.-(-.@e.)~))circumference<.@%p
end.
NB. set up for optimized version of above code
comb=. root (>:#]) }. spokes NB. candidate next primes to consider
discardp=. discard=. '' NB. what we'll be eliminating
for_p. comb do.
if. p e. comb =. comb (-. }.) discardp do.
NB. remove multiples of this next prime:
discardp=. p * spokes ( [{.~ >:@:(I.-(-.@e.)~))circumference<.@%p
discard =. discard, discardp
end.
end.
primes,comb,}.spokes-.discard
}}</syntaxhighlight>
 
Here, <code>pr 150</code> gives the same result as <code>pritchard 150</code> but <code>pr 1e7</code> takes well under a second.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
 
public final class SieveOfPritchard {
public static void main(String[] args) {
System.out.println(sieveOfPritchard(150) + System.lineSeparator());
System.out.println("Number of primes up to 1,000,000 is " + sieveOfPritchard(1_000_000).size() + ".");
System.out.println();
final long start = System.currentTimeMillis();
System.out.print("Number of primes up to 100,000,000 is " + sieveOfPritchard(100_000_000).size());
final long finish = System.currentTimeMillis();
System.out.println(". Obtained in a time of " + ( (double) finish - start ) / 1_000 + " seconds.");
}
private static List<Integer> sieveOfPritchard(int limit) {
List<Integer> primes = new ArrayList<Integer>();
BitSet members = new BitSet(limit + 1);
members.set(1);
List<Integer> deletions = new ArrayList<Integer>();
final int rootLimit = (int) Math.sqrt(limit);
int nLimit = 2;
int stepLength = 1;
int prime = 2;
while ( prime <= rootLimit ) {
if ( stepLength < limit ) {
for ( int w = 1; w >= 0; w = members.nextSetBit(w + 1) ) {
int n = w + stepLength;
while ( n <= nLimit ) {
members.set(n);
n += stepLength;
}
}
stepLength = nLimit;
}
deletions.clear();
int nextPrime = 5;
for ( int w = 1; w < nLimit; w = members.nextSetBit(w + 1) ) {
if ( nextPrime == 5 && w > prime ) {
nextPrime = w;
}
final int n = prime * w;
if ( n > nLimit ) {
break;
}
deletions.add(n);
}
for ( int deletion : deletions ) {
members.clear(deletion);
}
if ( nextPrime < prime ) {
break;
}
primes.add(prime);
prime = ( prime == 2 ) ? 3 : nextPrime;
nLimit = (int) Math.min((long) stepLength * prime, limit);
}
if ( stepLength < limit ) {
for ( int w = 1; w >= 0; w = members.nextSetBit(w + 1) ) {
int n = w + stepLength;
while ( n <= limit ) {
members.set(n);
n += stepLength;
}
}
}
members.clear(1);
for ( int i = members.nextSetBit(0); i >= 0; i = members.nextSetBit(i + 1) ) {
primes.add(i);
};
return primes;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149]
 
Number of primes up to 1,000,000 is 78498.
 
Number of primes up to 100,000,000 is 5761455. Obtained in a time of 0.648 seconds.
</pre>
 
=={{header|Julia}}==
Line 190 ⟶ 865:
""" Pritchard sieve of primes up to limit. Uses type of `limit` arg for type of primes """
function pritchard(limit::T, verbose=false) where {T<:Integer}
members = falses(limit * 2)
members[1] = true
steplength = 1 # wheel size
Line 199 ⟶ 874:
rc = 1 # removed count, since 1 will be removed at the end
rtlim = T(isqrt(limit)) # this allows the main loop to go
while prime <= rtlim # one extra time, eliminating the follow-up for
# the last partial wheel (if present)
if steplength < limit
for w in 1:length(members)steplength
if members[w]
n = w + steplength
Line 216 ⟶ 891:
np = 5
mcopy = copy(members)
for w in 1:length(members)nlimit
if mcopy[w]
np == 5 && w > prime && (np = w)
Line 236 ⟶ 911:
length(primes) + length(newprimes),
)
return sort!(append!(primes, newprimes))
end
 
Line 244 ⟶ 919:
up to 1000000, added 186825, removed 108494, prime count 78498
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="Nim">import std/[algorithm, math, sugar]
 
proc pritchard(limit: Natural): seq[int] =
## Pritchard sieve of primes up to "limit".
var members = newSeq[bool](limit + 1)
members[1] = true
var
stepLength = 1
prime = 2
rtlim = sqrt(limit.toFloat).int
nlimit = 2
primes: seq[int]
 
while prime <= rtlim:
if stepLength < limit:
for w in 1..members.high:
if members[w]:
var n = w + stepLength
while n <= nlimit:
members[n] = true
inc n, stepLength
stepLength = nlimit
 
var np = 5
var mcpy = members
for w in 1..members.high:
if mcpy[w]:
if np == 5 and w > prime:
np = w
let n = prime * w
if n > limit:
break # No use trying to remove items that can't even be there.
members[n] = false # No checking necessary now.
 
if np < prime:
break
primes.add prime
prime = if prime == 2: 3 else: np
nlimit = min(stepLength * prime, limit) # Advance wheel limit.
 
let newPrimes = collect:
for i in 2..members.high:
if members[i]: i
result = sorted(primes & newPrimes)
 
 
echo pritchard(150)
echo "Number of primes up to 1_000_000: ", pritchard(1_000_000).len
</syntaxhighlight>
 
 
{{out}}
<pre>@[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149]
Number of primes up to 1_000_000: 78498
</pre>
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
A console program written in Delphi 7. It follows the algorithm in the Wikipedia article "Sieve of Pritchard".
<syntaxhighlight lang="pascal">
program Pritchard_console;
 
{$APPTYPE CONSOLE}
 
uses
Math, SysUtils, Types;
 
// Function to return an array of all primes <= N
function PritchardSieve( const N : integer) : Types.TIntegerDynArray;
var
j, j_max, k, len, nrPrimes, p : integer;
marked : Types.TBooleanDynArray;
smallPrimes : Types.TIntegerDynArray; // i.e. primes <= sqrt( N)
spi : integer; // index into array smallPrimes
const
SP_STEP = 16; // step when extending dynamic array smallPrimes
begin
// Deal with trivial input
result := nil;
if (N <= 1) then exit;
 
// Initialize
SetLength( marked, N + 1); // 0..N for convenience; marked[0] is not used
marked[1] := true; // no other initialization of "marked" is needed
len := 1;
p := 2;
SetLength( smallPrimes, SP_STEP);
spi := 0;
 
while p*p <= N do begin
// Roll the wheel
if len < N then begin
j_max := Math.Min( p*len, N);
for j := len + 1 to j_max do marked[j] := marked[j - len];
len := j_max;
end;
 
// Unmark multiples of p
for k := len div p downto 1 do
if marked[k] then marked[p*k] := false;
 
// Store the prime p, extending the array if necessary
if spi = Length( smallPrimes) then
SetLength( smallPrimes, spi + SP_STEP);
smallPrimes[spi] := p;
inc(spi);
 
// Find the next prime p
if p = 2 then p := 3
else repeat inc(p) until (p > N) or marked[p];
// Condition p > N is a safety net; should always hit a marked value
Assert(p <= N);
end; // while
 
// Final roll, if needed. It is not needed if N >= 49. This is because
// 2 < 3^2, 2*3 < 5^2, 2*3*5 < 7^2, but thereafter 2*3*5*7 > 11^2, etc.
if len < N then
for j := len + 1 to N do marked[j] := marked[j - len];
 
// Remove 1 and put the small primes back
marked[1] := false;
for k := 0 to spi - 1 do marked[smallPrimes[k]] := true;
 
// Use the boolean array to return an array of prime integers
nrPrimes := 0;
for j := 2 to N do
if marked[j] then inc( nrPrimes);
SetLength( result, nrPrimes);
k := 0;
for j := 2 to N do
if marked[j] then begin result[k] := j; inc(k); end;
end;
 
// Main routine. User types the program name,
// optionally followed by the limit N (defaults to 150)
var
N, j : integer;
primes : Types.TIntegerDynArray;
begin
if ParamCount = 0 then N := 150
else N := SysUtils.StrToInt( ParamStr(1));
primes := PritchardSieve(N);
WriteLn( 'Number of primes = ', Length(primes));
for j := 0 to Length(primes) - 1 do begin
Write( ' ', primes[j]:4);
if j mod 10 = 9 then WriteLn;
end;
end.
</syntaxhighlight>
{{out}}
<pre>
Number of primes = 35
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149
</pre>
==={{header|Free Pascal}}===
see [[Sieve_of_Eratosthenes#alternative_using_wheel]]
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl" line>use v5.36;
use List::Util 'min';
 
my($limit, $maxS, $length, $p, @s) = (150, 1, 2, 3);
 
sub next_($w) { $s[$w-1] }
sub prev_($w) { $s[$w-2] }
 
sub append($w) {
$s[$maxS-1] = $w;
$s[$w-2] = $maxS;
$maxS = $w;
}
 
sub delete_multiples_of($p) {
my $f = $p;
while ($p*$f <= $length) { $f = next_ $f }
while ( $f > 1 ) { $f = prev_ $f; delete_pf($p*$f) }
}
 
sub delete_pf($pf) {
my($temp1, $temp2) = ($s[$pf-2], $s[$pf-1]);
$s[ $temp1-1 ] = $temp2;
$s[($temp2-2)%@s] = $temp1;
}
 
sub extend_to($n) {
my($w, $x) = (1, $length+1);
while ($x <= $n) {
append $x;
$w = next_ $w;
$x = $length + $w;
}
$length = $n;
append $limit+2 if $length == $limit
}
 
do {
extend_to min $p*$length, $limit if $length < $limit;
delete_multiples_of $p;
$p = next_ 1;
extend_to $limit if $length < $limit
} until $p*$p > $limit;
 
my @primes = 2;
for (my $p = 3; $p <= $limit; $p = next_ $p) { push @primes, $p }
say "@primes";</syntaxhighlight>
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149</pre>
 
=={{header|Phix}}==
Line 459 ⟶ 1,348:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./sort" for SortedList
import "./fmt" for Fmt
 
1,983

edits