Skip to content

Instantly share code, notes, and snippets.

@jc99kuo
Created May 11, 2018
Embed
What would you like to do?
FLOLAC 2018 Week 3 (2018-5-11 11:46) -- Goldbach's Conjecture
-- FLOLAC 2018 Week 3 -- Goldbach's conjecture
-- A simple, intuitive, straight fowward solution.
-- And may be not so efficient. :-)
goldbach :: Int -> Maybe (Int, Int)
goldbach nn
| nn < 3 = Nothing
| odd nn = Nothing
| otherwise =
if null pairs
then Nothing
else Just $ head pairs
where
pairs = [ (m,n) | m <- tryRange,
n <- tryRange,
(m + n) == nn ]
tryRange = takeWhile (\x -> x < nn) primeList
primeList = ppList [2 .. ]
ppList (n : ns) = n : ppList (filter (\x -> (mod x n) /= 0 ) ns)
-- Thanks to the call-by-need lazy evaluation,
-- The efficiency is really not bad. :-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment