Skip to content

Instantly share code, notes, and snippets.

@jc99kuo
Created May 11, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jc99kuo/339fd27d50ced55b19a062aa680b006b to your computer and use it in GitHub Desktop.
Save jc99kuo/339fd27d50ced55b19a062aa680b006b to your computer and use it in GitHub Desktop.
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