Last active
December 17, 2015 12:29
-
-
Save WillNess/5610477 to your computer and use it in GitHub Desktop.
primes7 - iterate TD by segments - http://stackoverflow.com/questions/16570644/last-init-faster-than-head-tail/16598738#16598738
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
primes4 :: [Integer] | |
primes4 = iterate | |
(\p -> ([x | | |
x <- [p + 1 ..], | |
all (\p -> x `mod` p /= 0) $ | |
takeWhile (\p-> p*p <= x) primes4 ] !! 0) | |
) | |
2 | |
primes5 :: [Integer] | |
primes5 = 2 : iterate (\p -> head [x | x <- [p + 2, p + 4 ..], | |
all (\p -> x `mod` p /= 0) $ tail $ | |
takeWhile (\p-> p*p <= x) primes5]) 3 -- n^2.0 | |
primes6 :: [Integer] | |
primes6 = 2 : iterate (\p -> -- slower 2x, interpreted, with "and" | |
head [x | x <- [p + 2, p + 4 ..], | |
and [ rem x p /= 0 | p<- takeWhile ((<= x).(^2)) $ tail primes6]]) 3 | |
-- why not more than just head, actually? ... | |
primes7a :: [Integer] | |
primes7a = concatMap snd $ | |
iterate (\((n,p:ps@(q:_)),_) -> ((n+1,ps), | |
[x | x <- [p*p + 2, p*p + 4 .. q*q - 2], | |
all ((/= 0).rem x) $ take n $ tail primes7a])) -- 50k: 1.91 secs n^1.4 | |
((1,tail primes7a),[2,3,5,7]) | |
primes7 :: [Integer] | |
primes7 = concatMap snd $ | |
iterate (\((n,p:ps@(q:_)),_) -> ((n+1,ps), -- 50k: 1.70s secs n^1.4 | |
[x | let lst = take n $ tail primes7, | |
x <- [p*p + 2, p*p + 4 .. q*q - 2], | |
all ((/= 0).rem x) $ lst])) | |
((1,tail primes7),[2,3,5,7]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment