CS 330 Home Schedule Resources

Homework 6
Recursive programming with integers

Estimated time: 3 hours

Acceptance tests for Assignment 6
About acceptance tests

The objective of this homework is to familiarize yourselves with functions and recursive procedures .

Exercises

  1. Triangular numbers. (15 points) Write a recursive function which calculates the sum of the first N positive integers, starting at 1.
    For example, {Sum 6} returns 21 = 1+2+3+4+5+6.

  2. Mirror, mirror on the wall... (15 points) There are two operators in OZ for dividing integers: div and mod .

    Write a recursive function Mirror which takes a positive integer N and returns the digits of the number in opposite order. For example, the call {Mirror 312} returns 213. Assume that N does not start or finish with 0 (zero). Naturally, div and mod are useful to write this function.

    Help: use an accumulator. What is the invariant satisfied by the function with accumulator?

  3. Parallel universe (first part). (10 points) In a parallel universe, one among you writes this code in answer to question 3.

       declare
    fun {Foo N}
    if N<10 then 1
    else 1+{Foo (N div 10)}
    end
    end
    The answer is correct...
  4. Parallel universe (second part). (10 points) Another coed answered with an alternative program. Its code is as follows.

       declare
    local
    fun {FooHelper N Acc}
    if N<10 then Acc+1
    else {FooHelper (N div 10) Acc+1}
    end
    end
    in
    fun {Foo N}
    {FooHelper N 0}
    end
    end
  5. Test of primality. (15 points) Write a function { Prime N } which returns true if the integer N is a prime number, false if not. Consider that N is prime if it is not divisible by any number K such that 2 <= K <N.

  6. Fibonacci (first part). (15 points) Write a naive recursive function Fibb which calculates the Nth Fibonacci number, given the following definition. (Ie., this version should not be tail call optimized.)

    fib(0) = 1
    fib(1) = 1
    fib(n) = fib(n-1) + fib(n-2) if N > 1
  7. One, two, buckle my shoe (first part). (5 points) A procedure is a succession of instructions which does not return a value. For example, a procedure which displays a number N and its successor can be defined by

       declare
    proc {BrowseNumber N}
    {Browse N}
    {Browse N+1}
    end

    Write a recursive procedure CountDown which takes an integer N and counts down from N to 0, by displaying each counted number. For example, { CountDown 3 } will display 

       3
    2
    1
    0
  8. One, two, buckle my shoe (second part). (10 points) Consider the procedure Count whose definition is given below. When this procedure is passed a positive integer N , it displays in the browser numbers 1 to N in ascending order.

       declare
    proc {Count N}
    local
    proc {CountFrom I}
    if I=<N then {Browse I} {CountFrom I+1} end
    end
    in
    {CountFrom 1}
    end
    end
    Notice that a recursive procedure CountFrom is defined inside the body of the procedure Count. It is this procedure which displays the numbers in order. Test this function, for example with { Count 10 }
  9. One, two, buckle my shoe (third part). (5 points) Now suppose one wants to modify the definition of Count so that the definition of CountFrom is outside the body of Count. Thus, it is no longer the body but instead the definition of Count that depends on CountFrom. The solution is very simple: to move the declaration of the instruction local outside Count.

       declare
    local
    proc {CountFrom I}
    if I=<N then {Browse I} {CountFrom I+1} end
    end
    in
    proc {Count N}
    {CountFrom 1}
    end
    end
    But it doesn't compile!

  10. Feedback.

Additional voluntary exercises

  1. Fibonacci (second part). Write a new version of Fibonacci FastFibb, where the number of recursive calls for N is in O(n) . Help: use a function with one (or several) accumulator parameters. What is the invariant of this function?

  2. Divisors and multiples. Into arithmetic, one frequently uses highest common factor between two whole numbers. One can define it as a function gcd with two integer parameters which satisfies the properties

    gcd(m, n) = gcd(n, m)
    gcd(m, n) = gcd(m+n, n)
    gcd(m, m) = m
    From the second property, one can derive
    gcd(m, n) = gcd(r, n) , where R is the remainder of the division of m by n.
    Implement a function GCD in OZ, by using these properties. This function takes two whole parameters. Help: Under quelle(s) condition is the derived property useful, i.e. R != m ?

    Implement a function LCM taking two whole parameters and returning their lowest common multiple . Re-use your function GCD.

  3. Classification of the points of the plan. There is a relatively simple technique to assign a natural number to each pair of natural numbers (x, y) . The following figure illustrates the technique, which consists in numbering the successive diagonals of the planar quadrant. The numbers of the points are in blue.

    Num�rotation des points du plan

    Write a function Number taking as parameter two natural numbers x and y , and returning their number according to the technique illustrated above. We propose two techniques.

  4. Under the paving stones... a pavement philosophizer poses the following question: `` How many ways can I pave a square surface whose sides are of integer length N with identical square paving stones of integer length and cover the whole square? '' the example below shows four possible pavings for n=6 . From left to right, paving stones whose side lengths are 6, 3, 2 and 1 were used.

    Pavings possible for n=6