summaryrefslogtreecommitdiff
path: root/31-41/41.hs
blob: 16fd51fbb8ebfdeb3488c71b6ad1b921a8042ac4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
isPrime :: Int -> Bool

isPrime 1 = False
isPrime x = and $ map (\y -> x `mod` y /= 0) $ fst $ span (\y -> y * y <= x) [2..]

goldbach :: Int -> (Int, Int)

goldbach x = head [(a, b) | a <- primes, let b = x - a, isPrime b]
    where primes = filterPrime[3..]
          filterPrime (p:xs) = p:filterPrime [x | x <- xs, x `mod` p /= 0]

goldbachList :: Int -> Int -> [(Int, Int)]
goldbachList' :: Int -> Int -> Int -> [(Int, Int)]

goldbachList l r = map goldbach $ filter even $ dropWhile (< 4) [l..r]
goldbachList' l r i = filter (\(a, b) -> a > i && b > i) $ goldbachList l r