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

Published on 12 May 2024 09:40 PM

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

Source code in the algol programming language

#
  Rosetta Code "Count the coins"
  This is a direct translation of the "naive" Haskell version, using an array
  rather than a list. LWB, UPB, and array slicing makes the mapping very simple:
 
  LWB > UPB     <=> []
  LWB = UPB     <=> [x]
  a[LWB a]      <=> head xs
  a[LWB a + 1:] <=> tail xs
#

BEGIN
  PROC ways to make change = ([] INT denoms, INT amount) INT :
  BEGIN
    IF amount = 0 THEN
      1
    ELIF LWB denoms > UPB denoms THEN
      0
    ELIF LWB denoms = UPB denoms THEN
      (amount MOD denoms[LWB denoms] = 0 | 1 | 0)
    ELSE
      INT sum := 0;
      FOR i FROM 0 BY denoms[LWB denoms] TO amount DO
        sum +:= ways to make change(denoms[LWB denoms + 1:], amount - i)
      OD;
      sum
    FI
  END;
  [] INT denoms = (25, 10, 5, 1);
  print((ways to make change(denoms, 100), newline))
END

#
   Rosetta Code "Count the coins"
   This uses what I believe are the ideas behind the "much faster, probably
   harder to read" Haskell version.  
#

BEGIN
   PROC ways to make change = ([] INT denoms, INT amount) LONG INT:
   BEGIN
      [0:amount] LONG INT counts, new counts;

      FOR i FROM 0 TO amount DO counts[i] := (i = 0 | 1 | 0) OD;

      FOR i FROM LWB denoms TO UPB denoms DO
         INT denom = denoms[i];
         FOR j FROM 0 TO amount DO new counts[j] := 0 OD;
         FOR j FROM 0 TO amount DO
            IF LONG INT count = counts[j]; count > 0 THEN
               FOR k FROM j + denom BY denom TO amount DO
                  new counts[k] +:= count
               OD
            FI;
            counts[j] +:= new counts[j]
         OD
      OD;
      counts[amount]
   END;

   print((ways to make change((1, 5, 10, 25), 100), newline));
   print((ways to make change((1, 5, 10, 25, 50, 100), 10000), newline));
   print((ways to make change((1, 5, 10, 25, 50, 100), 100000), newline))
END

  

You may also check:How to resolve the algorithm Word wrap step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Color quantization step by step in the Go programming language
You may also check:How to resolve the algorithm Map range step by step in the COBOL programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Racket programming language
You may also check:How to resolve the algorithm Dot product step by step in the Clojure programming language