How to resolve the algorithm Count the coins step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Count the coins step by step in the OCaml programming language
Table of Contents
Problem Statement
There are four types of common coins in US currency:
There are six ways to make change for 15 cents:
How many ways are there to make change for a dollar using these common coins? (1 dollar = 100 cents).
Less common are dollar coins (100 cents); and very rare are half dollars (50 cents). With the addition of these two coins, how many ways are there to make change for $1000? (Note: the answer is larger than 232).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Count the coins step by step in the OCaml programming language
Source code in the ocaml programming language
let changes amount coins =
let ways = Array.make (amount + 1) 0L in
ways.(0) <- 1L;
List.iter (fun coin ->
for j = coin to amount do
ways.(j) <- Int64.add ways.(j) ways.(j - coin)
done
) coins;
ways.(amount)
let () =
Printf.printf "%Ld\n" (changes 1_00 [25; 10; 5; 1]);
Printf.printf "%Ld\n" (changes 1000_00 [100; 50; 25; 10; 5; 1]);
;;
You may also check:How to resolve the algorithm Synchronous concurrency step by step in the Elixir programming language
You may also check:How to resolve the algorithm Pathological floating point problems step by step in the AWK programming language
You may also check:How to resolve the algorithm Zhang-Suen thinning algorithm step by step in the Java programming language
You may also check:How to resolve the algorithm Median filter step by step in the Python programming language
You may also check:How to resolve the algorithm Menu step by step in the F# programming language