Find minimum number of coins that make a given value: Difference between revisions
Find minimum number of coins that make a given value (view source)
Revision as of 04:14, 12 August 2021
, 2 years ago→{{header|Haskell}}: Allowed for denomination lists which are ill-sorted or incomplete.
(→{{header|Haskell}}: Added a variant (hand-written recursion, rather than standard recursion scheme)) |
(→{{header|Haskell}}: Allowed for denomination lists which are ill-sorted or incomplete.) |
||
Line 108:
(1,1)</pre>
Or as a hand-written recursion, defining a slightly more parsimonious listing
<lang haskell>
import Data.Ord (Down (Down))
---------- MINIMUM NUMBER OF COINS TO MAKE A SUM ---------
change
change
|
| otherwise =
where
go _ 0 = []
go (x : xs) n▼
| 0 == q = go xs n
| otherwise = (q, x) : go xs r
where
(q, r) = quotRem n x
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ putStrLn $ test <$> [1024, 988]▼
where
test n =
▲ mapM_ putStrLn $
( \qvs
( \(q,
(change [200, 100, 50, 20, 10,
▲ )
{{Out}}
<pre>Summing to 1024:
Line 155 ⟶ 160:
1 * 5
1 * 2
1 * 1</pre>
=={{header|Julia}}==
|