Find minimum number of coins that make a given value: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Case where smallest denomination leaves an unsummed residue)
Line 117: Line 117:
change :: [Int] -> Int -> Either String [(Int, Int)]
change :: [Int] -> Int -> Either String [(Int, Int)]
change units n
change units n
| 1 `elem` units = Right $ go (sortOn Down units) (abs n)
| 0 == mod n m = Right $ go (sortOn Down units) (abs n)
| otherwise = Left "Incomplete denominations - lacks 1"
| otherwise =
Left $
concat
[ "Residue of ",
show (mod n m),
" - no denomination smaller than ",
show m,
"."
]
where
where
m = minimum units
go _ 0 = []
go _ 0 = []
go (x : xs) n
go (x : xs) n
Line 144: Line 153:
qvs
qvs
)
)
(change [200, 100, 50, 20, 10, 5, 2, 1] n)</lang>
(change [200, 100, 50, 20, 10, 5] n)</lang>
{{Out}}
{{Out}}
<pre>Summing to 1024:
<pre>Summing to 1024: