With plenty of pennies, nickels and dimes in my pockets I can buy a cup of coffee for $1.50 in any possible way. In how many different ways a cup of coffee can be bought with the exact amount of money ($1.50)?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Very simple approach. Nice.
A Wolfram Mathematica 11.3 function: RationalQ := $#$1 ∈ Q & ; which tests whether its argument is a rational number.
Another function: change ( amount$_$ /; RationalQ ( amount ) , coins$__$ /; AllTrue [ coins , RationalQ ] ∧ AllTrue [ coins , Positive ] ) := Block [ { c , amt , tokens } , c ( amt$_$ , { } ) := 0 /; amt = 0 ; c ( 0 , _ ) := 1 ; c ( amt$_$ , tokens$_$ ) := 0 /; amt < 0 ; c ( amt$_$ , tokens$_$ ) := c ( amt , tokens ) = c ( amt − First [ tokens ] , tokens ) + c ( amt , Rest [ tokens ] ) ; c ( amount , Select [ Sort [ Union [ coins ] , Greater ] , $#$1 ≤ amount & ] ) ]
A function named change .
First argument, called amount , must be a rational number.
Second argument, called coins , must be a list of positive, rational numbers,
is defined as
A local scope with two local variables, amt and tokens .
If amt is not equal to 0 and the remaining list of coins is empty, then c[amt,{}] is defined as 0
If amt is 0, regardless of the remaining coins, then c[0,_] is defined as 1
If amt < 0, then c[amt ,tokens ] is defined as 0
Otherwise, c[amt ,tokens ] is defined as the memo-ed value of that combination of values which is
sum of two invocations of c: c with the amount reduced by the first coin and the full list of coins and c with the full amount and list with the first coin dropped.
Invoke c with the initial and the coins sorted in descending order and the coins which are too large to be useful to be removed.
Close the local scope.
change ( 1 0 0 1 5 0 , { 1 0 0 1 , 1 0 0 5 , 1 0 0 1 0 } ) = 2 5 6
change ( 1 0 0 , { 1 0 0 0 , 5 0 0 , 1 0 0 , 5 0 , 2 0 , 1 0 , 5 , 2 , 1 , 2 1 , 4 1 , 1 0 1 , 2 0 1 , 1 0 0 1 } ) is 9 8 2 3 5 4 6 6 6 1 9 0 6 .
In words, nine trillion, eight hundred twenty-three billion, five hundred forty-six million, six hundred sixty-one thousand, nine hundred six.
Problem Loading...
Note Loading...
Set Loading...
If I decide to include D dimes in my payment (with 0 ≤ D ≤ 1 5 ), then I have exactly 3 1 − 2 D ways to pay up: I can use anywhere from 0 to 5 1 5 0 − 1 0 D = 3 0 − 2 D nickels and pay the rest in pennies. Thus the answer is ∑ D = 0 1 5 ( 3 1 − 2 D ) = 1 6 2 = 2 5 6 .