-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem046.hs
26 lines (20 loc) · 933 Bytes
/
problem046.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
-- It turns out that the conjecture was false.
-- What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
import Data.Foldable (find)
import qualified Data.Map as M
import qualified Control.Monad
import Data.Maybe (fromJust)
import Primes (primes)
main :: IO ()
main = putStrLn $ maybe "Goldbach was right!" show (find notGoldbachs odds)
odds :: [Integer]
odds = [3, 5 ..]
member :: Ord a => a -> [a] -> Bool
member x xs = x `elem` takeWhile (<= x) xs
notGoldbachs :: Integer -> Bool
notGoldbachs = Control.Monad.ap ((&&) . not . goldbachs) (not . (`member` primes))
goldbachs :: Integer -> Bool
goldbachs x = any (`member` primes) remainders where
remainders = map (x - ) $ takeWhile (< x) doubledSquares
doubledSquares = map ((2 *) . (^ 2)) [1..]