Subset sum problem: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: non-bruteforce)
Line 501: Line 501:


None bruteforce: the list of numbers used here are different, and difficult for a bruteforce method.
None bruteforce: the list of numbers used here are different, and difficult for a bruteforce method.
<lang haskell>subsum ls = sub_ [(0,[])] ls where
<lang haskell>subsum = foldl sub_ [(0,[])] where
sub_ a [] = a
sub_ a x = merge a $ map f a where
sub_ a (x:xs) = sub_ (merge a $ map (f x) a) xs
f (a,l) = (a+x, l++[x])
f x (a,l) = (a+x, l++[x])


-- keep list of sums sorted. Don't use standard sort!
-- keep list of sums sorted. Don't use standard sort!