We introduce the change() and display() functions. With change() we compute possible arrangements of coins until we meet our goal amount. We only add equal or larger coins.
def change(coins: List[Int], amounts: List[Int], highest: Int, sum: Int, goal: Int): Unit = {
// See if we have reached the total sum of coins.
// ... Display our results if we have.
if (sum == goal) {
display(coins, amounts)
}
else if (sum < goal) {
// We still need to add coins.
for (value <- amounts) {
// Only add higher or equal coins than before.
if (value >= highest) {
// Add the new value to the start of a list.
val copy = value :: coins
// Continue adding new coins.
change(copy, amounts, value, sum + value, goal)
}
}
}
}
def display(coins: List[Int], amounts: List[Int]): Unit = {
// Loop over all coin sizes.
for (amount <- amounts) {
// Count total number of coins of that type.
val count = coins.count(_ == amount)
// Display our coins.
println(s
"$amount: $count")
}
println()
}
object Program {
def main(args: Array[String]): Unit = {
// We start with no coins.
val coins = List()
// Coin sizes.
val amounts = List(1, 5, 10, 25, 50)
// Begin counting change.
change(coins, amounts, 0, 0, 51)
}
}
1: 51
5: 0
10: 0
25: 0
50: 0
1: 46
5: 1
10: 0
25: 0
50: 0
...
1: 1
5: 0
10: 5
25: 0
50: 0
1: 1
5: 0
10: 0
25: 2
50: 0
1: 1
5: 0
10: 0
25: 0
50: 1