("union" removed, appears on Q.31)
Revision as of 11:32, 5 June 2011
(*) A list of prime numbers.
Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.
primesR :: Integral a => a -> a -> [a] primesR a b | even a = filter isPrime [a+1,a+3..b] | True = filter isPrime [a,a+2..b]
If we are challenged to give all primes in the range between a and b we simply take all numbers from a up to b and filter all the primes through.
This is good for very narrow ranges as Q.31's
isPrime tests numbers by trial division using (up to) a memoized primes list produced by sieve of Eratosthenes to which it refers internally. So it'll be slower, but immediate, testing the numbers one by one.
For very wide ranges, specifically when , we're better off just using the primes sequence itself, without any post-processing:
primes :: Integral a => [a] primes = primesTME -- of Q.31 primesR :: Integral a => a -> a -> [a] primesR a b = takeWhile (<= b) $ dropWhile (< a) primes
Another way to compute the claimed list is done by using the Sieve of Eratosthenes.
primesR :: Integral a => a -> a -> [a] primesR a b = takeWhile (<= b) $ dropWhile (< a) $ sieve [2..] where sieve (n:ns) = n:sieve [ m | m <- ns, m `mod` n /= 0 ]
sieve [2..] function call generates a list of all (!) prime numbers using this algorithm and
primesR filters the relevant range out. [But this way is very slow and I only presented it because I wanted to show how nicely the Sieve of Eratosthenes can be implemented in Haskell :)]
this is of course a famous case of (mislabeled) executable specification, with all the implied pitfalls of inefficiency when (ab)used as if it were an actual code.
Use the proper Sieve of Eratosthenes from e.g. 31st question's solution (instead of the above sieve of Turner), adjusted to start its multiples production from the given start point:
-- tree-merging Eratosthenes sieve, primesTME of Q.31, -- adjusted to produce primes in a given range (inclusive) primesR a b | b<a || b<2 =  | otherwise = (if a <= 2 then  else ) ++ (takeWhile (<= b) $ gaps a' $ join [[x,x+step..] | p <- takeWhile (<= z) primes' -- p^2 <= b , let q = p*p ; step = 2*p x = snap (max a' q) q step ]) where primes' = tail primesTME -- external unbounded list of primes a' = snap (max 3 a) 3 2 z = floor $ sqrt $ fromIntegral b + 1 join (xs:t) = union xs (join (pairs t)) join  =  pairs (xs:ys:t) = (union xs ys) : pairs t pairs t = t gaps k xs@(x:t) | k==x = gaps (k+2) t | True = k : gaps (k+2) xs snap v origin step = let r = rem (v-origin) step -- v >= origin in if r==0 then v else v+(step-r)
It should be much better then taking a slice of a full sequential list of primes, as it won't try to generate any primes between the square root of b and a. To wit,
> primesR 10100 10200 -- Sol.4 [10103,10111,10133,10139,10141,10151,10159,10163,10169,10177,10181,10193] (4,656 reductions, 10,859 cells) > takeWhile (<= 10200) $ dropWhile (< 10100) $ primesTME -- Sol.2 [10103,10111,10133,10139,10141,10151,10159,10163,10169,10177,10181,10193] (140,313 reductions, 381,058 cells) > takeWhile (<= 10200) $ dropWhile (< 10100) $ sieve [2..] -- Sol.3 where sieve (n:ns) = n:sieve [ m | m <- ns, m `mod` n /= 0 ] [10103,10111,10133,10139,10141,10151,10159,10163,10169,10177,10181,10193] (54,893,566 reductions, 79,935,263 cells, 6 garbage collections) > filter isPrime [10101,10103..10200] -- Sol.1 [10103,10111,10133,10139,10141,10151,10159,10163,10169,10177,10181,10193] (12,927 reductions, 24,703 cells) -- isPrime: Q.31
(testing with Hugs of Nov 2002).
This solution is faster but not immediate. It has a certain preprocessing stage but then goes on fast to produce the whole range. To illustrate, to produce the 49 primes in 1000-wide range above 120200300100 it takes about 18 seconds on my oldish notebook for the 1st version, with the first number produced almost immediately (~ 0.4 sec); but this version spews out all 49 primes in one go after just under 1 sec.