Fibonacci
Fibonacci
The Fibonacci numbers (sequence A000045 at the OEIS) are generally defined
using the recurrence relation Fn = Fn−1 + Fn−2 , with F0 = 0 and F1 = 1. Thus,
the first ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
The most obvious way to compute Fibonacci numbers is recursively, using
the recurrence relation directly. Another, more efficient way (an example of
“dynamic programming”) is to start at the beginning and keep track of the two
most recently-computed values, iteratively computing the next until the desired
point is reached. Finally, like every sequence defined by linear recurrence, the
Fibonacci numbers have a closed-form
√
solution, and Fn can be computed directly
φn 1+ 5
by rounding √ 5
(where φ = 2 is the golden ratio) to the nearest integer.
Define three functions, each of which takes an index n as an argument, and
returns Fn , computed recursively, iteratively, or directly with the closed-form
solution respectively. Use the following header:
fibonacci.h
1 unsigned int fibonacci_recursive(unsigned int n);
2 unsigned int fibonacci_iterative(unsigned int n);
3 unsigned int fibonacci_closed(unsigned int n);
Put each function in its own file; for example, implement fibonacci iterative
in fibonacci iterative.c. In a file named fibonacci.c, write a main func-
tion that prints out the first ten Fibonacci numbers computed by each function
in a table with columns for recursive, iterative, and closed-form, from left to
right. Since the functions should obviously all return the same values, the cor-
rect output is:
$ ./fibonacci
0 0 0
1 1 1
1 1 1
2 2 2
3 3 3
5 5 5
8 8 8
13 13 13
21 21 21
34 34 34
If you name your files as indicated, the following Makefile will enable you
to build your program by simply typing make.
Makefile
1
1 CFLAGS=-Wall -g
2 LDFLAGS=-lm
4 OBJECTS=\
5 fibonacci.o \
6 fibonacci_recursive.o \
7 fibonacci_iterative.o \
8 fibonacci_closed.o
10 fibonacci: $(OBJECTS)
11 fibonacci.o: fibonacci.c fibonacci.h
12 fibonacci_recursive.o: fibonacci_recursive.c fibonacci.h
13 fibonacci_iterative.o: fibonacci_iterative.c fibonacci.h
14 fibonacci_closed.o: fibonacci_closed.c fibonacci.h
16 .PHONY: clean
17 clean:
18 rm -f fibonacci $(OBJECTS)
For the closed-form computation, you will need to cast the unsigned integer
argument to double to maintain the required√precision. You will want to know
and use the functions sqrt, for computing 5; pow, for computing φn ; and
round, for correctly rounding the final result to the nearest integer before cast-
ing it back to unsigned int and returning it. All of those functions are found
in math.h, and require linking with the math library libm (thus the special
LDFLAGS in the Makefile).
None of these functions requires more than a handful of lines of code; al-
though there might be a lot of thinking involved, if you actually find yourself
writing a lot of code, perhaps you are approaching the problem the wrong way.
On CMS, submit your source files, fibonacci.c, fibonacci_recursive.c,
fibonacci_iterative.c, and fibonacci_closed.c; when grading, I will pro-
vide the fibonacci.h header file and the Makefile as they appear above.