Here is the code I am trying to use: This should generate all primes up to 100
sieve_primes = [x | x<-[2..100], y<-[2..50], z <-[2..25], (x*z) `mod` y /= 0]
Here is the code I am trying to use: This should generate all primes up to 100
sieve_primes = [x | x<-[2..100], y<-[2..50], z <-[2..25], (x*z) `mod` y /= 0]
The code
isPrime n = length [x | x<-[2..n], n `mod` x == 0] == 1
computes all the factors just to count them. You don't need to count them: as soon as the second factor is found you can stop your search without checking for further ones.
So, either replace length ... == 1
with a custom predicate, or take 2
elements from the list comprehension before checking its length.
What you had in mind was probably
This is very slow. At least we can rearrange the pieces,
This is still very slow, for any number much above even 1000 (where you'd use 500 and 250). Then again, why the 25 (250) limit? Your code follows the
idea, i.e.
y*z = 2*y .. min (y*y) x
, so with the known top limit (x <= n
) it should be(incidentally,
max (min y (n/y)) {y=2..n/2} = sqrt n
, so we could've used 10 instead of 25, (and 31 instead of 250, for the 1000)).Now 1000 is not a problem, only above ~ 10,000 we again begin to see that it's slow (still), running at n2.05..2.10 empirical orders of growth (quick testing interpreted code in GHCi at n = 5000, 10000, 15000).
As for your second (now deleted) function, it can be rewritten, step by step improving its speed, as
now, compiled, it can get first 10,000 primes in few tenths of a second.