Prolog Exercise 1
Informal Description
The aim of this exercise is to solve problems of the following type:
Assuming that it is possible to give a person Lm. N, where
N is
some amount between 0 and L,
how many different ways are there to distribute a total amount?
Lm. L to P persons.
In particular:
- Solve the problem for N=0-9 P=2
- Solve the problem for N=0-9 P=3-5
To answer this question you need only material from
tutorial 1. In particular it does not require
knowledge of rules. The problem will posed to Prolog in the form of a
conjoined query.
The answer will not appear directly, but will be
implicit in the number of solutions obtained.
How to Proceed
Part 1
The first part can be solved directly by creating a set of facts that
completely encodes the the relation sum(X,Y,N)
between two numbers X, Y
and their sum N. The facts for N=1 are:
sum(0,1,1). i.e. (0 + 1 = 1)
sum(1,0,1). i.e. (1 + 0 = 1)
If the facts are loaded into the database,
the query
?- sum(P1,P2,1).
encodes the problem for P=2 and N=1. There are clearly two solutions.
Complete the answer to part 1 by encoding facts for N=0, N=2-9 and testing.
Part 2
The second part is solved by merely formulating the appropriate
conjoined queries to express the P-component sum. Hence for
P=3, we have P1+P2+P3=N.
Only follow this link
if you have thought about how to proceed and emerged clueless.
Mike Rosner (mros@cs.um.edu.mt)
Last modified: Thu May 15 17:07:38 MET DST