Sieve of Eratosthenes: Difference between revisions
m
→With Wheel: Haskell - minor touch-ups and comments...
GordonBGood (talk | contribs) (→With Wheel: Haskell Tree-Folding: minor edits...) |
GordonBGood (talk | contribs) m (→With Wheel: Haskell - minor touch-ups and comments...) |
||
Line 5,625:
The above example has been left to show its problems, as follows:
1. The generation of the wheel works just to generate a small wheel list
2. The above routine uses ridiculous means to re-
<lang haskell>-- autogenerates wheel primes, first sieve prime, and gaps
Line 5,663:
x : (union xs . _U . pairs) t where -- tree-shaped folding big union
pairs (xs:ys:t) = union xs ys : pairs t -- ~= nub . sort . concat
union xs@(x:xs') ys@(y:ys')
| otherwise = x : union xs' ys' -- x and y must be equal!
Note that the "wheelGen" code could be used to not need to do further culling at all by continuously generating wheels until the square of the "firstSievePrime| is greater than the range as there are no composites left up to that limit, but this is always slower than a SoE due to the high overhead in generating the wheels - this would take a wheel generation of 1229 (number of primes to the square root of a hundred thousand is ten thousand) to create the required wheel sieved to a hundred million; however, the theoretical (if the time to advance through the lists per element were zero, which of course it is not) asymptotic performance would be O(n) instead of O(n log (log n)) where n is the range sieved. Just another case where theory supports (slightly) reduced number of operations, but practicality means that the overheads to do this are so big as to make it useless for any reasonable range ;-) !
===Priority Queue based incremental sieve===
|