[Haskell-cafe] how to make haskell faster than python at finding primes?

Alex Jacobson alex at alexjacobson.com
Mon Aug 6 04:15:46 EDT 2007


The challenge was the implement the modcount algorithm not to calculate 
primes per se.
(see e.g. http://jjinux.blogspot.com/2005/11/io-comparison.html).

-Alex-

Donald Bruce Stewart wrote:
> alex:
>> This implementation of calculating 10000 primes (compiled with GHC -O2) 
>> is 25% slower than the naive python implementation.  Shouldn't it be 
>> faster?  Am I doing something obviously stupid?
> 
> Why not just:
> 
>     primes         = sieve [2..]
> 
>     sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p > 0]
> 
>     main           = print (take 1000 primes)
> 
> That's super naive, and seems to be around 5x faster than the code you were
> trying. (So make sure you're doing the same thing as the python version)
>     
> -- Don 



More information about the Haskell-Cafe mailing list