{-# OPTIONS_GHC -O2 #-} -- unbounded merging idea due to Richard Bird
module Main where -- double staged production idea due to Melissa O'Neill
-- tree folding idea Dave Bayer / Heinrich Apfelmus
main = -- simplified formulation by Will Ness
do s
<- getLine -- logBase (n2/n1) (t2/t1): ~ n^1.20
primes
= 2 :
_Y
((3:
) . gaps
5 . _U
. map (\x
-> [x
*x
, x
*x
+2*x
..]))
_Y g = g (_Y g) -- telescoping feed (recursive)
-- = g xs where xs = g xs -- double feed (corecursive, sharing)
gaps k s@(x:xs) | k < x = k : gaps (k+2) s -- ~= [k,k+2..]\\s ,
| otherwise = gaps
(k
+2) xs
-- when s⊂[k,k+2..]
_U ((x:xs):t) = x : (union xs . _U . pairs) t -- ~= nub.sort.concat
where
pairs (xs:ys:t) = union xs ys : pairs t
union a
@(x:xs
) b
@(y:ys
) = case compare x y
of LT -> x : union xs b
EQ -> x : union xs ys
GT -> y : union a ys
ey0jIE9QVElPTlNfR0hDIC1PMiAjLX0gIC0tIHVuYm91bmRlZCBtZXJnaW5nIGlkZWEgZHVlIHRvIFJpY2hhcmQgQmlyZAptb2R1bGUgTWFpbiB3aGVyZSAgICAtLSBkb3VibGUgc3RhZ2VkIHByb2R1Y3Rpb24gaWRlYSBkdWUgdG8gTWVsaXNzYSBPJ05laWxsCiAgICAgICAgICAgICAgICAgICAgICAgLS0gdHJlZSBmb2xkaW5nIGlkZWEgRGF2ZSBCYXllciAvIEhlaW5yaWNoIEFwZmVsbXVzCm1haW4gPSAgICAgICAgICAgICAgICAgICAtLSBzaW1wbGlmaWVkIGZvcm11bGF0aW9uIGJ5IFdpbGwgTmVzcwogIGRvIHMgPC0gZ2V0TGluZSAgICAgICAgICAgICAgICAgICAgICAgLS0gbG9nQmFzZSAobjIvbjEpICh0Mi90MSk6IH4gbl4xLjIwCiAgICAgcHJpbnQgJCB0YWtlIDUgJCBkcm9wIChyZWFkIHMtNSkgcHJpbWVzIAoKcHJpbWVzIDo6IFtJbnRdICAgICAgICAgIApwcmltZXM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