Primality by trial division: Difference between revisions

revert my wrong edit, add comments
(→‎Segmented Generate and Test: comment, for clarity)
(revert my wrong edit, add comments)
Line 1,071:
 
primesST = 2 : 3 : sieve 5 9 (drop 2 primesST) (inits $ tail primesST) where
sieve x q ps (fs:ft) = filter (noDivsBy\y-> all ((/=0).mod y) fs) [x,x+2..q-2] -- fs = [], [3], [3,5], ...
++ sieve (q+2) (head ps^2) (tail ps) ft</lang>
<code>inits</code> makes a stream of (progressively growing) prefixes of an input stream, starting with an empty prefix., here making the <code>noDivsByfs</code> isparameter to get a sequence of values <code>[], [#Haskell|defined3], above][3,5], ...</code>.
 
Runs at empirical <math>O(n^{1.4...})</math> time complexity, in ''n'' primes produced. Can be used as a framework for unbounded segmented sieves, replacing divisibility testing with proper sieve of Eratosthenes on arrays, etc.
 
The filtering function is equivalent to <code>noDivsBy</code> [[#Haskell|defined above]], except that the comparisons with the square root are no longer needed and so are spared.
 
=={{header|HicEst}}==
751

edits