Created
December 22, 2012 19:37
-
-
Save masak/4360642 to your computer and use it in GitHub Desktop.
This is the whole Haskell code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.MultiSet as MultiSet | |
import qualified Data.Set as Set | |
limit :: Int | |
limit = 100 | |
products :: MultiSet.MultiSet Int | |
products = MultiSet.fromList [ x * y | x <- [2..limit], y <- [x+1..limit-x] ] | |
sums_with_unambiguous_products :: Set.Set Int | |
sums_with_unambiguous_products = Set.fromList [ x + y | x <- [2..limit], y <- [x+1..limit-x], MultiSet.occur (x * y) products == 1 ] | |
products_causing_S_not_to_know :: MultiSet.MultiSet Int | |
products_causing_S_not_to_know = MultiSet.fromList [ p | s <- [2..limit], Set.notMember s sums_with_unambiguous_products, x <- [2..(s `div` 2)+1], let p = x * (s - x), MultiSet.occur p products > 1 ] | |
ambiguous_sums :: MultiSet.MultiSet Int | |
ambiguous_sums = MultiSet.fromList [ s | s <- [2..limit], Set.notMember s sums_with_unambiguous_products, x <- [2..(s `div` 2)+1], let p = x * (s - x), MultiSet.occur p products_causing_S_not_to_know == 1 ] | |
solutions :: [(Int, Int)] | |
solutions = [ (x, y) | s <- [2..limit], x <- [2..(s `div` 2)+1], let y = s - x ] | |
main :: IO () | |
main = mapM_ print solutions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment