1. import Data.List -- stackoverflow.com/q/33531834
2. import Data.Function (fix)
3.
4. main1 = print . take 100 . nubBy (\x y -> x `rem` y == 0) \$ [2..]
5. main2 = print . take 100 . nubBy (\x y -> x `gcd` y > 1) \$ [2..]
6.
7. -- stackoverflow.com/a/36668089
8. -- stackoverflow.com/a/36725003
9. foldi1 f (h:t) = f h . foldi1 f . unfoldr (\(a:b:r)-> Just (f a b, r)) \$ t
10.
11. ps1=2 : fix ((3:) . concat . unfoldr (\(x,u:us)-> Just ([x|u==0], (x+2,us)))
12. . (,) 5 . ([0,0] ++)
13. . foldi1 (\(x:xs) ys -> let n=div (head ys - x) 2 - 1 in
14. x:take n xs ++ zipWith max ys (drop n xs))
15. . map (\p-> (p*p :) . tail . cycle \$ 1 : replicate (p-1) 0) )
16.
17. main = print . take 10 . drop 80000 \$ ps -- 40k ps1:1.88s ps:1.20s
18. -- 80k 5.52 3.68
19. -- which is to say, -- n^1.6 n^1.6
20.
21. no_compos x (u:us) = [x | u==0] ++ no_compos (x+2) us
22. foldi f (h:t) = f h . foldi f . pairs f \$ t
23. pairs f (a:b:r) = f a b : pairs f r
24. mjoin (x:xs) ys = x : after (div (head ys - x) 2 - 1) xs (zipWith max ys)
25. after n xs k | n>0 = head xs : after (n-1) (tail xs) k
26. | otherwise = k xs
27. mults p = (p*p :) . tail . cycle \$ 1 : replicate (p-1) 0
28. ps = 2 : ops where
29. ops = 3:5:7:(no_compos 9 . foldi mjoin . map mults) ops
Success #stdin #stdout 3.68s 7768KB
stdin
Standard input is empty
stdout
[1020389,1020401,1020407,1020413,1020419,1020431,1020451,1020457,1020491,1020517]