import Data.Numbers.Primes (primes, isPrime)
import Data.List (genericReplicate)
-- 1ª definición
-- =============
a249624 :: [Integer]
a249624 = 0: 1: [x | x <- [2,4..], primo (x + siguientePrimo x)]
primo :: Integer -> Bool
primo x = [y | y <- [1..x], x `rem` y == 0] == [1,x]
siguientePrimo :: Integer -> Integer
siguientePrimo x = head [y | y <- [x+1..], primo y]
-- 2ª definición (por recursión)
-- =============================
a249624b :: [Integer]
a249624b = 0 : 1 : 2: aux [2,4..] primos where
aux (x:xs) (y:ys)
| y < x = aux (x:xs) ys
| (x+y) `pertenece` ys = x : aux xs (y:ys)
| otherwise = aux xs (y:ys)
pertenece x ys = x == head (dropWhile (<x) ys)
primos :: [Integer]
primos = 2 : [x | x <- [3,5..], primo x]
-- 3ª definición (con la librería de primos)
-- =========================================
a249624c :: [Integer]
a249624c = 0: 1: [x | x <- [2,4..], isPrime (x + siguientePrimo3 x)]
siguientePrimo3 x = head [y | y <- [x+1..], isPrime y]
-- 4ª definición (por recursión con la librería de primos)
-- =======================================================
a249624d :: [Integer]
a249624d = 0 : 1 : 2: aux [2,4..] primes where
aux (x:xs) (y:ys)
| y < x = aux (x:xs) ys
| (x+y) `pertenece` ys = x : aux xs (y:ys)
| otherwise = aux xs (y:ys)
pertenece x ys = x == head (dropWhile (<x) ys)
-- 5ª definición
-- =============
a249624e :: [Integer]
a249624e = [a | q <- primes,
let p = siguientePrimo3 (q `div` 2),
let a = q-p,
siguientePrimo3 a == p]
-- 6ª definición
-- =============
a249624f :: [Integer]
a249624f = [x | (x,y) <- zip [0..] ps, isPrime (x+y)]
where ps = 2:2:concat (zipWith f primes (tail primes))
f p q = genericReplicate (q-p) q
-- 7ª definición
-- =============
a249624g :: [Integer]
a249624g = 0:1:(aux primes (tail primes) primes)
where aux (x:xs) (y:ys) zs
| null rs = aux xs ys zs2
| otherwise = [r-y | r <- rs] ++ (aux xs ys zs2)
where a = x+y
b = 2*y-1
zs1 = takeWhile (<=b) zs
rs = [r | r <- [a..b], r `elem` zs1]
zs2 = dropWhile (<=b) zs
-- ---------------------------------------------------------------------
-- § Comparación de eficiencia --
-- ---------------------------------------------------------------------
-- La comparación es
-- ghci> :set +s
--
-- ghci> a249624 !! 700
-- 5670
-- (12.72 secs, 1245938184 bytes)
--
-- ghci> a249624b !! 700
-- 5670
-- (8.01 secs, 764775268 bytes)
--
-- ghci> a249624c !! 700
-- 5670
-- (0.22 secs, 108982640 bytes)
--
-- ghci> a249624d !! 700
-- 5670
-- (0.20 secs, 4707384 bytes)
--
-- ghci> a249624e !! 700
-- 5670
-- (0.17 secs, 77283064 bytes)
--
-- ghci> a249624f !! 700
-- 5670
-- (0.08 secs, 31684408 bytes)
--
-- ghci> a249624g !! 700
-- 5670
-- (0.03 secs, 4651576 bytes)