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

Published on 12 May 2024 09:40 PM
#J

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

Source code in the j programming language

merge=: ({:"1 (+/@:({."1),{:@{:)/. ])@;
count=: {.@] <@,. {:@] - [ * [ i.@>:@<.@%~ {:@]
init=: (1 ,. ,.)^:(0=#@$)
nsplits=: 0 { [: +/ [: (merge@:(count"1) init)/ }.@/:~@~.@,


   100 nsplits 1 5 10 25
242


   100000 nsplits 1 5 10 25 50 100
13398445413854501


   init =: 4 : '(1+x)$1'
length1 =: 4 : '1=#y'
      f =: 4 : ',/ +/\ (-x) ]\ y'

      1000 {  f ` init @. length1 / 1000 500 200 100 50 20 10 5 2 , 1000 0
327631322

NB. this is a foldLeft once initialised the intermediate right arguments are arrays
 1000 f 500 f 200 f 100 f 50 f 20 f 10 f 5 f 2 f (1000 init 0)


  

You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Burlesque programming language
You may also check:How to resolve the algorithm Pi step by step in the Haskell programming language
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the Phix programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Action! programming language