You are on page 1of 1

COMP303-Fall 2017_Assignment # 1

# 1 (a) Write a program to calculate the nth Fibonacci number using both recursive and
iterative algorithm. Your program will have the following specifications:

It will take in a number (n) as input from user and compute the nth Fibonacci number.

It will also compute the time taken by both algorithms to generate the result respectively.

In the end, your result will resemble the following:

Input:

Specify a number for computation: 3

Output:

3 Fibonacci number is: 1

Time taken to compute the result is

Iterative method: 0.001 sec

Recursive method: 0.002 sec

[These are not calculated results, hence no need to panic if your answers dont match with these
(in fact it would be surprising if they did) as they just serve as an example].

(b) Use the program to calculate the Fibonacci number for different number (n). Draw a graph of
the time taken by both algorithms as n is increased, with time on y-axis and n on x-axis.

[Plot the results for both the algorithms on the same graph].

# 2 (a) Show whether f(n) is Big O of g(n) or not, in the following cases.

f(n) g(n)

n-100 n-200

n3 + 20n + 1 n2

3n2 + 4n + 1 n2

7n4 + 3n2 + 5n + 1000 7n4

You might also like