Week 3: Exercises

1. Addition

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.

2. Multiplication

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.

3. Factorial

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.

4. Sum of First N Elements

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.

5. Slice

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.

6. Day Count

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).

7. Day Count, Extended

Extend the previous exercise so that it correctly computes the count for any years in the Gregorian calendar.

8. Greatest Common Divisor

Write a predicate gcd(I, J, K) that is true if the greatest common divisor of I and J is K.

9. Prime

Write a predicate is_prime(N) that is true if N is prime.

10. All Primes

Write a predicate all_primes(I, J) that returns a list of all prime numbers between I and J, inclusive.

11. Multi-Duplicate

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.

12. Smallest Prime Factor

Write a predicate smallest_factor(N, P) that is true if P is the smallest prime factor of N.

13. Drop Every Nth

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.

14. Number of Prime Factors

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.

15. Bubble Sort

Write a predicate that sorts a list of integers. Use an bubble sort.

16. Insertion Sort

Write a predicate that sorts a list of integers. Use an insertion sort.

17. Mini-Minesweeper, Revisited

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.

18. Vertical Minesweeper

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.

19. Beethoven's Wig

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:

  1. The green key goes to the third or fourth box.

  2. The wig is to the left of the fourth box.

  3. The wig is to the right of the first box.

  4. The yellow key is to the left of the wig.

  5. The blue key is to the right of the yellow key and to the left of the green key.

  6. The red key goes to the first box.

Which box contains Beethoven's wig? Write a Prolog program that can find the answer.

20. Cryptarithmetic

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.