// Recursively compute fibonnaci sequence, using a
|
// Recursively compute fibonnaci sequence, using a
|
// really inefficient algorithm.
|
// really inefficient algorithm.
|
// (Stack exercise test)
|
// (Stack exercise test)
|
|
|
#include "tv80_env.h"
|
#include "tv80_env.h"
|
|
|
int answers[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
|
int answers[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
|
233, 377, 610, 987, 1597, 2584, 4181 };
|
233, 377, 610, 987, 1597, 2584, 4181 };
|
|
|
int fib (int n)
|
int fib (int n)
|
{
|
{
|
int rv;
|
int rv;
|
timeout_port = 0x02;
|
timeout_port = 0x02;
|
|
|
if (n < 2) rv = n;
|
if (n < 2) rv = n;
|
else rv = fib(n-1) + fib(n-2);
|
else rv = fib(n-1) + fib(n-2);
|
|
|
timeout_port = 0x01;
|
timeout_port = 0x01;
|
return rv;
|
return rv;
|
}
|
}
|
|
|
int main ()
|
int main ()
|
{
|
{
|
int fn, fr;
|
int fn, fr;
|
char pass;
|
char pass;
|
|
|
set_timeout (60000);
|
set_timeout (60000);
|
pass = 1;
|
pass = 1;
|
|
|
for (fn = 1; fn < 20; fn++) {
|
for (fn = 1; fn < 20; fn++) {
|
print ("Computing Fibonacci number ");
|
print ("Computing Fibonacci number ");
|
print_num (fn);
|
print_num (fn);
|
print ("\n");
|
print ("\n");
|
|
|
fr = fib(fn);
|
fr = fib(fn);
|
print ("Number is: ");
|
print ("Number is: ");
|
print_num (fr);
|
print_num (fr);
|
|
|
if (fr == answers[fn-1]) {
|
if (fr == answers[fn-1]) {
|
print (" (correct)\n");
|
print (" (correct)\n");
|
} else {
|
} else {
|
print (" (incorrect)\n");
|
print (" (incorrect)\n");
|
print ("Correct result: ");
|
print ("Correct result: ");
|
print_num (answers[fn-1]);
|
print_num (answers[fn-1]);
|
pass = 0;
|
pass = 0;
|
print ("\n");
|
print ("\n");
|
}
|
}
|
}
|
}
|
|
|
if (pass)
|
if (pass)
|
sim_ctl (SC_TEST_PASSED);
|
sim_ctl (SC_TEST_PASSED);
|
else
|
else
|
sim_ctl (SC_TEST_FAILED);
|
sim_ctl (SC_TEST_FAILED);
|
}
|
}
|
|
|