Suppose that we represent natural numbers using structures: 0 = z, 1 = s(z), 2 = s(s(z)) and so on.
Write a predicate sum(I, J, K)
that is true if I
+ J = K, where I, J,
and K have this numeric representation. Your predicate should work in
all directions.
Extending the previous exercise, write
a predicate mul(I, J, K)
that is true if I
ยท J = K, where I, J, and K have this numeric representation. Your
predicate should work in all directions.
Write a predicate
factorial(N, F)
that
is true if F = N! . Your predicate should work in all directions and
should terminate when the solution set is finite.
Write a predicate
sum_n(N, L, S)
that
is true if S is the sum of the first N elements of L. If L has fewer
than N elements, the predicate should fail.
Write a predicate slice(L, I, J, M)
that is true if M
contains elements I .. J of L, where elements are indexed
starting from 1. For example, slice([r, i, v, e, r], 2,
4, [i, v, e]) is true.
Write a predicate total_days(Y, Z, N)
that is true if
N is the total number of days in the years Y .. Z.
Assume that 1900 < Y, Z < 2100 (which makes leap year
calculations easier).
Extend the previous exercise so that it correctly computes the count for any years in the Gregorian calendar.
Write a predicate gcd(I, J, K)
that is true if the
greatest common divisor of I and J is K.
Write a predicate is_prime(N)
that is true if N is
prime.
Write a predicate all_primes(I, J)
that returns a
list of all prime numbers between I and J, inclusive.
Write a predicate multidup
that duplicates the
elements of a list K times. For example, multidup([s, k, y], 3,
[s, s, s, k, k, k, y, y, y])
is true.
Write a predicate smallest_factor(N, P)
that is true
if P
is the smallest prime factor of N
.
Write a predicate drop(L, N, M)
that is true if we
can remove every Nth element from L to make M. For example, drop([a,
b, c, d, e, f, g], 3, [a, b, d, e, g])
is true.
Write a predicate
num_factors(A, N)
that
is true if A
has
exactly N
prime
factors, where repeated factors are counted separately. For example,
num_factors(12, 3)
is
true since 12 = 2 x 2 x 3.
Write a predicate that sorts a list of integers. Use an bubble sort.
Write a predicate that sorts a list of integers. Use an insertion sort.
Consider the following tiny Minesweeper board of dimensions 5 x 2:
1? 2? 2? 2? 1?
A number N means that there are N mines in adjacent squares (which may be adjacent horizontally or diagonally).
Write a Prolog program that can find all possible positions for the mines. Use integer constraints.
Generalize the previous exercise as follows. Consider a Minesweeper board of dimensions N x 2, where the left column contains integers and the contents of all squares in the right column is unknown. Write a predicate minesweeper(L, M), where L is the integers in the left column, and M is a list of values 'mine' or 'no_mine'. For example, minesweeper([1, 2, 2, 2, 1], M) will give a solution to the previous exercise.
Someone has stolen Beethoven's wig and has put it in one of four locked boxes. The boxes are numbered 1,2,3,4 in that order. There are four different keys that each have their own color. Also:
The green key goes to the third or fourth box.
The wig is to the left of the fourth box.
The wig is to the right of the first box.
The yellow key is to the left of the wig.
The blue key is to the right of the yellow key and to the left of the green key.
The red key goes to the first box.
Which box contains Beethoven's wig? Write a Prolog program that can find the answer.
In this cryptarithmetic puzzle, every letter stands for a different digit:
A P P L E + G R A P E + P L U M =========== B A N A N A
Additionally, no leading digit may be zero.
Write a Prolog program that can find a solution to the puzzle.