1, 1, 2, 3, 5, 8, 13, 21
What is the ``structure'' of the sequence? If you stare at it for a
sufficiently long time and/or if you already know something about
Fibonacci sequences, you'll say something like this: Each number is the
sum of the previous two numbers.
More formally, what this means is that the above sequence is a second order recurrence relation with the structure
F(n) = F(n-1) + F(n-2) // structure
F(0) = 1, F(1) = 1 // initial condition
We will consider recurrence relations having a restricted structure. The
structure of our recurrence relations is
F(n) = F(n-1) + g(n)
where g(n) is a function of n. The permissible
form of g(n) is defined recursively as follows.
g(n) = 1 is a permissible form
g(n) = n is a permissible form
g(n) is a permissible form,
then
g(n)+1,
g(n)+n,
g(n)-1,
g(n)-n,
g(n)*n,
and g(n)^n are permissible forms.
g(n).
1
(n-1)*n
2n (= n+n or (1+1)*n)
n^2 (= n*n)
2^n (= (1+1)^n)
The problem is, given an example sequence, can you output the next two
numbers? For example, consider the sequence, 1, 2, 4, 7, 11, 16.
In this case,
the function g(n) = n. And the sequence is
F(0) = 1; F(1) = F(0) + g(1) = 1 + 1 = 2 F(2) = F(1) + g(2) = 2 + 2 = 4 F(3) = F(2) + g(3) = 4 + 3 = 7 F(4) = F(3) + g(4) = 7 + 4 = 11 F(5) = F(4) + g(5) = 11 + 5 = 16 F(6) = F(5) + g(6) = 16 + 6 = 22 F(7) = F(6) + g(7) = 22 + 7 = 29Note that
F(n) = F(n-1) + n with
F(0) = 1. The next two numbers are: 37, 46..
(The same sequence is
also given by F(n) = n(n-1)/2 + 1. You can't write
F(n) as g(n) for any permissible g(n). This is where recurrence relations
help.)
g(n)
structs, unions, and pointers.
Another way of solving this is to create functions, evaluate them, and to test them on the the sequences. The evaluation part is easy in this case since we can use ML itself to evaluate the functions. It is not possible to create functions in C, but functions can be created in ML.
One way to compare imperative and functional programming is to write code in C and ML and then compare the codes. But the fact is it is difficult to write such imperative code in C. ML provides better support for creating and evaluating expressions!
The function which does this should be called evalExprComplete.
It should take an integer list as argument and return an integer list
containing the next two integers in the sequence.
This function should be called evalFunComplete.
completeSeq.ml and use bundleP2
to submit the assignment.
Your program should use O(dB) space where d is the depth at which the solution is found and B is the branching factor.
Some test sequences:
1, 0, 0, 9, 41, 116, 260, ... [g(n) = (n-2)n^2] 1, 0, 0, 1, 17, 260, ... [g(n) = (n-2)^n] 10, 8, 12, 4, 20, ~12, ... [g(n) = (-2)^n]
bundleP2 before 7:00 PM
on Feb 11, 2000.
You should submit your report, a hard copy of your code and test results, and the self-evaluation form to Renata Teixeira between 6:00 - 7:00 PM on Feb 11, 2000 at APM 3349D.
Absolute academic honesty is required.