Title: RE: Odd Performance
You may also like to compare this definition.
> import List
>
> primes = unfoldr sieve [2..]
> sieve (x:xs) = Just (x,filter (x `notDivisor`) xs)
> x `notDivisor` y = y `mod` x /= 0
Slower, but (IMO) cuter.
Cheers,
-Original Message-
Tom Pledger writes:
> Probably. Try replacing this
>(\z -> z <= (intsqrt x))
> with this
>(\z -> z^2 <= x)
Yes! This is significantly nicer. Taking 4000 primes, this is about twice
as fast as the original (loose) algorithm, and it appears that it gets
better as n grows. (Call this versio
Tom Pledger writes:
| Tim Otten writes:
| :
| | Can anyone suggest why the tighter algorithm exhibits significantly
| | worse performance? Is takeWhile significicantly more expensive than
| | take?
|
| No.
Correction (before anyone else pounces on it):
Only if the predicate function (
Tim Otten writes:
:
| Can anyone suggest why the tighter algorithm exhibits significantly
| worse performance? Is takeWhile significicantly more expensive than
| take?
No.
| Is the \z lambda expression expensive?
No.
| The intsqrt isn't recalculated each time takeWhile evalutes a
| prime
As a student in an undergraduate 'Intro to Discrete Structures' course, I
recently did a project which required generating the first n primes. We
discussed the sieve of Eratosthenes in class. Although the professor is
not familiar with Haskell, he allowed me to use it, and I
mistakenly wrote the fo