How to resolve the algorithm Count the coins step by step in the Scilab programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Count the coins step by step in the Scilab 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 Scilab programming language

Source code in the scilab programming language

amount=100;
coins=[25 10 5 1];
n_coins=zeros(coins);
ways=0;

for a=0:4
    for b=0:10
        for c=0:20
            for d=0:100
                n_coins=[a b c d];
                change=sum(n_coins.*coins);
                if change==amount then
                    ways=ways+1;
                elseif change>amount
                    break
                end
            end
        end
    end
end

disp(ways);


function varargout=changes(amount, coins)
    ways = zeros(1,amount + 2);
    ways(1) = 1;
    for coin=coins
        for j=coin:(amount+1)
            ways(j+1) = ways(j+1) + ways(j + 1 - coin);
        end
    end
    
    varargout=list(ways(length(ways)))
endfunction

a=changes(100, [1, 5, 10, 25]);
b=changes(100000, [1, 5, 10, 25, 50, 100]);
mprintf("%.0f, %.0f", a, b);


  

You may also check:How to resolve the algorithm Polynomial long division step by step in the Octave programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Oforth programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the Fortran programming language
You may also check:How to resolve the algorithm Perfect totient numbers step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Function definition step by step in the Cowgol programming language