RosettaCodeData/Task/Count-the-coins/D/count-the-coins-2.d
2015-02-20 00:35:01 -05:00

21 lines
581 B
D

import std.stdio, core.checkedint;
auto changes(int amount, int[] coins, ref bool overflow) {
auto ways = new ulong[amount + 1];
ways[0] = 1;
foreach (coin; coins)
foreach (j; coin .. amount + 1)
ways[j] = ways[j].addu(ways[j - coin], overflow);
return ways[amount];
}
void main() {
bool overflow = false;
changes( 1_00, [25, 10, 5, 1], overflow).writeln;
if (overflow)
"Overflow".puts;
overflow = false;
changes( 1000_00, [100, 50, 25, 10, 5, 1], overflow).writeln;
if (overflow)
"Overflow".puts;
}