Prolog Exercise 1
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
some amount between 0 and L,
how many different ways are there to distribute a total amount?
Lm. L to P persons.
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
The answer will not appear directly, but will be
implicit in the number of solutions obtained.
- Solve the problem for N=0-9 P=2
- Solve the problem for N=0-9 P=3-5
How to Proceed
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,
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.
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 (firstname.lastname@example.org)
Last modified: Thu May 15 17:07:38 MET DST