| CS 330 | Home | Schedule | Resources |
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 .
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.
Sum.Sum called SumIt (for "iterative") which uses an
accumulator acc . The invariant for the helper function is sum(n)
= sum(i) + acc . Mirror, mirror on the wall... (15 points) There are two operators in
OZ for dividing integers: div and mod .
div returns the whole part (quotient) of the division
of two integers. Therefore, 11 div 4 is 2.
mod returns the remainder of the division
of two integers. Therefore, 11 mod 4 is 3.
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?
Parallel universe (first part). (10 points) In a parallel universe, one among you writes this code in answer to question 3.
declareThe answer is correct...
fun {Foo N}
if N<10 then 1
else 1+{Foo (N div 10)}
end
end
{ Foo 123456 } return?{ Foo 3211 } return?
{ Foo 0 } return?
{ Foo ~ 666 } return?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
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.
N
is not divisible by any K such that M <=
K <N. Integer M is a parameter of
this function. N.
Indeed, if N= k*l with K <= L ,
then K 2 <= N. Modify your auxiliary
function accordingly and rename Prime to FastPrime. 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
N = 1 N = 4
N = 5
N = 8
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
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.
declareNotice that a recursive procedure
proc {Count N}
local
proc {CountFrom I}
if I=<N then {Browse I} {CountFrom I+1} end
end
in
{CountFrom 1}
end
end
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 }
CountFrom
created? Count is created.
Count, i.e. its free identifiers and their value?
CountFrom?
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.
declareBut it doesn't compile!
local
proc {CountFrom I}
if I=<N then {Browse I} {CountFrom I+1} end
end
in
proc {Count N}
{CountFrom 1}
end
end
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?
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)From the second property, one can derive
gcd(m, n) = gcd(m+n, n)
gcd(m, m) = m
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.
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.
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.
Express the number of a point according to the number of its predecessor.
The predecessor is defined behind while following the blue arrows. For
example, the successive predecessors of (3, 2) are (4, 1) ,
(5, 0) , (0, 4) , (1, 3) ... Implement an
iterative version of Number.
Improve the effectiveness of your function on the basis of two following observations. First of all, a point (X, y) is on the same diagonal as the point (x+y, 0) . Then, the number of a point on the x axis, of the form (X, 0) , corresponds to the number of points in the triangle (0, 0) , (x-1, 0) , (0, x-1) . And this number is a triangular number ...
Being given a number N , can you find the co-ordinates (x, y) of the corresponding point? Help: Find initially co-ordinate X by comparing N with the numbers of the points of the type (X, 0) . Then, calculate the co-ordinate y. Are your functions iterative? What is their invariant?
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.
Write a function NumberPaves which takes N
in parameter and returns the number of pavings in question.
Suppose now that the paver carries out all possible pavings.
How many paving stones will it have used? In the n=6 example , there
is 1+4+9+36=50 of it . Write a function NumberPaves
who takes N in parameter and returns this number.