-- goldbach conjecture program -- read in an even number and find two primes which sum to it import Text.Printf -- test if a number is prime starting at a given number prime_from value test = if value <= test then True else if (mod value test) == 0 then False else (prime_from value (test + 1)) -- test if a number is prime isPrime value = prime_from value 2 -- generate a list of prime numbers gen_primes largest = [x | x <- [2 .. largest], isPrime x] -- loop through the primes for one that works goldbach_recurse value primes = let a = head primes in let b = value - a in if (isPrime b) then [a, b] else (goldbach_recurse value (tail primes)) -- call the above function and show results goldbach value = goldbach_recurse value (gen_primes value) -- main IO action main = do -- read in input as a String putStrLn "Enter an even number: " line <- getLine -- convert it to an Int let number = read line :: Int -- get the answer and print it let [a, b] = goldbach number printf "Two prime numbers that add to %d are %d and %d.\n" number a b