We use cookies to improve your experience, for authentication and to ensure that we show you advertising that is relevant to you. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on Ideone website. However, if you wish, you can change cookie settings of your browser at any time.
{-# OPTIONS_GHC -O2 #-}-- unbounded merging idea due to Richard Birdmodule Main where-- double staged production idea due to Melissa O'Neill-- tree folding idea Dave Bayer / simplified formulation Will Ness
main =do s <-getLineprint$take5$drop(read s-5) primes -- logBase (n2/n1) (t2/t1): O(n^1.20)
primes ::[Int]-- first corecursive algorithm in known human history:
primes =2 : g (fix g)-- creates its argument while analyzing its outputwhere
g xs =3 : gaps 5(unionAll [[x*x, x*x+2*x..]| x <- xs])
fix g = xs where xs = g xs -- global defn to avoid space leak
gaps k s@(x:xs)| k<x = k:gaps (k+2) s -- (== [k,k+2..]`minus`s ,k<=x| True = gaps (k+2) xs -- (-fno-full-laziness : no leak) )
unionAll ((x:xs):t)= x : union xs (unionAll (pairs t))where
pairs ((x:xs):ys:t)=(x : union xs ys) : pairs t
union a@(x:xs) b@(y:ys)=casecompare x y of
LT -> x : union xs b
EQ -> x : union xs ys
GT -> y : union a ys
union a b =ifnull a then b else a