99 questions/Solutions/41

From HaskellWiki
Jump to navigation Jump to search

(**) Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.

In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the range 2..3000.

goldbachList lb ub = map goldbach $ [even_lb,even_lb+2..ub]
  where even_lb = max ((lb+1) `div` 2 * 2) 4
goldbachList' lb ub mv = filter (\(a,b) -> a > mv && b > mv) $
                         goldbachList lb ub

using the goldbach function from problem 40.

Or a more concise version:

goldbachList n m = map goldbach $ dropWhile (<4) $ filter even [n..m]
goldbachList' n m i = filter (\(x,y) -> x > i && y > i) $ goldbachList n m

Note the dropWhile, as the question explicitly asks for the range 1-2000 (although we know better).