Count the coins: Difference between revisions

Content added Content deleted
Line 3,849: Line 3,849:
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
fn main() {
fn main() {
amount := 100000
amount := 100
coins := [1, 5, 10, 25]
coins := [25, 10, 5, 1]
println("amount: $amount; ways to make change: ${count_2(coins, amount)}")
println("amount: $amount; ways to make change: ${count_2(coins, amount)}")
}
}


fn count(coins []int, amount int) int {
fn count(coins []int, amount int) int {
mut arr := []int{len: amount + 1}
mut ways := []int{len: amount + 1}
arr[0] = 1
ways[0] = 1
for coin in coins {
for coin in coins {
for idx := coin; idx <= amount; idx++ {
for idx := coin; idx <= amount; idx++ {
arr[idx] += arr[idx - coin]
ways[idx] += ways[idx - coin]
}
}
}
}
return arr[amount]
return ways[amount]
}
}
</syntaxhighlight>
</syntaxhighlight>
Output:
Output:
<pre>
<pre>
amount: 100000; ways to make change: 279364825
amount: 100; ways to make change: 242
</pre>
</pre>