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

Published on 12 May 2024 09:40 PM

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

Source code in the forth programming language

\ counting change (SICP section 1.2.2)

: table create does> swap cells + @ ;
table coin-value 0 , 1 , 5 , 10 , 25 , 50 ,

: count-change ( total coin -- n )
  over 0= if
    2drop 1
  else over 0< over 0= or if
    2drop 0
  else
    2dup coin-value - over recurse
    >r 1- recurse r> +
  then then ;

100 5 count-change .


  

You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the Picat programming language
You may also check:How to resolve the algorithm Fibonacci word/fractal step by step in the jq programming language
You may also check:How to resolve the algorithm Delete a file step by step in the VBScript programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the Groovy programming language
You may also check:How to resolve the algorithm Generator/Exponential step by step in the Mathematica / Wolfram Language programming language