Question: Please write it in Prolog Write a Prolog program for a predicate change/3 such that change(Cs,X,Ns) holds if and only if - Cs is a

Please write it in Prolog

Write a Prolog program for a predicate change/3 such that change(Cs,X,Ns) holds if and only if - Cs is a list of nonnegative integers representing the denominations of coins, for example if Cs = [10,5,2], this means that we consider 10p, 5p and 2p coins.

- X is a nonnegative integer representing an amount of money.

- Ns is a list of nonnegative integers, of the same length as Cs, saying how the amount X can be composed from the available coins.

For example, the query ?- change([10,5,2],17,Ns).

should yield the answers

Ns = [1, 1, 1] ;

Ns = [0, 3, 1] ;

Ns = [0, 1, 6] ;

false.

since 17 = 1 10 + 1 5 + 1 2 = 0 10 + 3 5 + 1 2 = 0 10 + 1 5 + 6 2

and there is no other way of writing 17 as the sum of (nonnegative integer) multiples of 10, 5 and 2.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!