Complexity Study On Fibonacci New

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

1

Complexity Study on Fibonacci’s Sequence

Abstract

This report deals with algorithmic complexity used in the determination of Fibonacci’s

sequence term. While exposing three correct algorithms, we have, in the light of complexity

study of each one of them, released the one that is optimal. Numbers of Fibonacci’s sequence

being for the majority very large, we preferred the using of computer to determine them.

History

Leonardo Fibonacci first proposed the sequence of numbers named in his honor in his

book Liber Abaci (Book of the Abacus). The rabbit problem was an exercise in addition and not

an attempt to accurately model a rabbit population. It appears that the relation fn = fn+1+ fn+2 was

not recognized by Fibonacci but was first noted by Albert Girard in 1634. In 1844 G. Lame

became the first person to use the Fibonacci numbers in a "practical" application when he used

the Fibonacci sequence to prove the number of divisions needed to find the greatest common

divisor of two positive integers using the Euclidean algorithm does not exceed five times

the number of digits in the smaller of the two integers. 1 The use of the term "Fibonacci

numbers" was initiated by E. Lucas in the 1870's. Many relations among the Fibonacci and

related numbers are due to Lucas and a recurring series first proposed by Lucas has taken his

name. The Lucas numbers are defined as:

Ln = Ln-1 + Ln-2 for n>1, L0 = 2, L1 = 1

Another series, that appears later in this thesis, first described by Lucas is:

Βn+1= βn2-2, β1= 3

giving the series 3, 7, 47, ....


2

1.1 Introduction

If we look around us, we see that computers and computer networks are everywhere,

activating a fabric of complex human activities: education, trade, entertainment, research, health,

communication, and even war. One of the two technological factors causing this astonishing

proliferation, is obviously the blowing speed with which the projections in micro-electronics and

design of chips took us to increasingly fast hardware. However, though becoming increasingly

fast, computers do not manage to solve certain problems for which the algorithms are however

known. This is due to the complexity of the latter.

1.2 Information about Algorithm

The word "algorithm" comes from the Latin word (Algorismus) taken after the name of

Arab mathematician ALKHAREZMI or Al-Khwarizmi, author of a handbook of popularization

on Indian positional decimal calculation (about 830 A.D.) explaining its use and, especially, the

handling of various algorithms that allow to carry out traditional arithmetic operations (addition,

subtraction, multiplication, division, extraction of square roots, rule of three, etc.)

Definition 01 (Algorithm)

In mathematics and computer science, an algorithm is a finite sequence of well-defined,

computer-implementable instructions, typically to solve a class of problems or to perform a

computation. Algorithms are unambiguous specifications for performing calculation, data

processing, automated reasoning, and other tasks.

As an effective method, an algorithm can be expressed within a finite amount of space

and time, and in a well-defined formal language for calculating a function.


3

Definition 2 (Program)

A program is the realization (implementation) of an algorithm by means of a given

language (on a given architecture). It is about the implementation of the principle. For example,

during the programming, one will sometimes explicitly deal with the memory management

(dynamic allocation into C Language for example) which is an ignored problem of

implementation at the algorithmic level.

Time complexity

In computer science, the time complexity is the computational complexity that describes

the amount of time it takes to run an algorithm. Time complexity is commonly estimated by

counting the number of elementary operations performed by the algorithm, supposing that each

elementary operation takes a fixed amount of time to perform. Thus, the amount of time taken

and the number of elementary operations performed by the algorithm are taken to differ by at

most a constant factor. The time complexity is generally expressed as a function of the size of the

input. Since this function is generally difficult to compute exactly, and the running time for small

inputs is usually not consequential, one commonly focuses on the behavior of the complexity

when the input size increases—that is, the asymptotic behavior of the complexity. Therefore, the

time complexity is commonly expressed using big O notation, typically O(1), O(n) , O(log n),

O(2n) etc., where n is the input size in units of bits needed to represent the input. Algorithmic

complexities are classified according to the type of function appearing in the big O notation.

2. Methods of Computation

There are many ways to compute Fibonacci numbers. Each section of this report will

describe one method (and closely related methods) of computing the Fibonacci numbers.
4

The following algorithms in this report have been seen in print:

 Natural recursive in section 2.1,

 Repeated addition or iteration in section 2.2,

 Binet's formula or explicit form in section 2.3

2.1 Natural recursive

Several introductory programming texts introduce recursion by computing the Fibonacci

numbers. The function fib(n) that computes f(n) will return zero if n = 0, one if n = 1 and the sum

of fib (n-1) and fib (n-2) if n > 2, is proposed as the solution.

C++ program

int recfib (int n) _________________________________________________ f(n)

if (n<=1) ___________________________________________________ 1

return n;

return recfib(n-1) + recfib(n-2); ___________________________ f(n-1) + f(n-2)

Time complexity

T(n)= T(n-1) + T(n-2) + c ___ c= 4 or order of 1

BIG-O

O (2n)
5

Graph and Table

Following fig 2.1 will show the execution of recursive program of finding nth Fibonacci

number. For different values of n. The time is noted in microseconds and three different readings

were taken. Average of three reading show the actual Time complexity of this approach. The

graph represents three readings as well as the average of the recursive function Time complexity.

Fig (2.1)
6

2.2 REPEATED ADDITION OR ITERATIVE METHOD

If, instead of first computing f(n-1) and f(n-2) as was done in the recursive solution,each

element of the sequence f0, f1, f2…, fn was computed in order, the number of operations could be

reduced. The algorithm presented in figure 2.2 computes Fibonacci numbers in this manner.

C++ program

int Fib (int n) ________________________________________________________ T(n)

if((n==0) ||(n==1)) ________________________________________________ 1

return n; ___________________________________________________ 1

else

int prev_Fib=0, curr_Fib=1, next_Fib;

while(n>=2) _________________________________________________ n

next_Fib = prev_Fib + curr_Fib; _________________________ n

prev_Fib = curr_Fib; _________________________ n

curr_Fib = next_Fib; _________________________ n

n--;

}
7

return curr_Fib; ________________________________________________ 1

Time complexity

T (n) = 4n + 1

BIG-O

O (n)

Graph and Table

Following fig 2.2 will show the execution of iterative program of finding nth Fibonacci

number. For different values of n. The time is noted in microseconds and three different readings

were taken. Average of three readings show the actual Time complexity of this approach. The

graph represents three readings as well as the average of the iterative Fibonacci Time

complexity.
8

Fig (2.2)

2.3 Binet's formula or explicit form

While the following is usually called Binet's formula for fn, Knuth [19] credits Abraham

de Moivre with discovering that a closed form for fn, is:


9

1 1 + √5 1 1 − √5
𝑓(𝑛) = ( )− ( )
√5 2 √5 2

C++ program:

int fib (int n) ________________________________________________ T(n)

double x=5, result;

double phi = (1 + sqrt(x)) / 2; __________________________ 1

return (pow (phi, n) / sqrt(x)); _________________________ 1

Time complexity

T (n) = 1, (sqrt() and pow() has O (log n)

BIG-O

O (1)

Graph and Table

Following fig 2.3 will show the execution of explicit program of finding nth Fibonacci

number. For different values of n the time is noted in microseconds and three different readings

were taken. Average of three readings show the actual Time complexity of this approach. The

graph represents three readings as well as the average of the explicit Fibonacci Time complexity.
10

Fig (2.3)
11

3. Conclusion and Results

Even though we have the algorithm, there are three questions that we ask on this subject:

1. Is this algorithm correct?

2. How much time is it taken like function of n?

3. Can we better do?

The first question is debatable (it might not be asked) more especially as the algorithm, as

well as the corresponding program, is precisely the Fibonacci’s definition of . But the second

question deserves an answer.

Let T (n) be the number of operations which the computer needs to carry out to calculate

F (n).

What can we say about this function? For not informed readers, if is lower than 2, the

procedure stops almost immediately after two operations. Thus, T(n) ≤ 2,∀ n ≤ 1.

For great values of n, there are two recursive invocations of F (n) repeated T (n-1)and

T(n-2) 2 times respectively, plus three operations of the computer (2 checks on the value of and

the final addition).

Then T (n) = T(n-1) +T(n-2) +3 ∀ n>1

Let us compare this result with the recursive relation of fn. We see immediately that. T(n)

≥ fn This is a bad news: The execution time of the algorithm grows as quickly as Fibonacci’s

numbers! T(n) is exponential out of n, which implies that the algorithm is practically slow except

for very small values of n.

Let us be a little more concrete on the disadvantage of an exponential growth. To

calculate F200, the F (n) algorithm carries out T (n) ≥ F200 ≥ 2138 elementary operations of the

computer. Time that takes depends of course on the computer used. Until 2006, Dasgupta et al.
12

[12, 13] affirm that the fastest computer in the world was the NEC Earth Simulator whose speed

was 40. 10 12 elementary operations per second. Even on this computer, F (n)(200) would take

at least 292 seconds ( 151 x1020 years). This means that, if we begin calculation today, it would

continue even after the sun is transformed into an enormous red star.

But technology develops quickly: Speed of computers doubles every 18 months, a

phenomenon sometimes called "Moore’s law". With this extraordinary growth, perhaps F (n)

will be carried out more quickly by the future years machines. Cottet-Emard and Goetgheluck

[14] remain pessimistic on this subject: Indeed, the execution time of F (n) is proportional to

20.694 n ≈ (1.6)n, thus one will need 1,6 times more time to calculate than . And under the Moore’s

law, computers become 1,6 times faster each year. Thus, if we can reasonably calculate with this

year technology, then the next year we will calculate. And the following year , and so on: just a

Fibonacci’s number moreover each year! Such is the misfortune of exponential time.

In short, our naive and recursive algorithm is correct but hopelessly ineffective. We will need, to

solve this problem, to resort to another algorithm.

3.2 Graphical representation of Time complexity of three approaches of Fibonacci series

This graph represents the time complexity Big O of three different approaches used to

determine nth number of Fibonacci series. YELLOW line represents time complexity of explicit

approach. Grey line on the graph shows time complexity of iterative approach. Orange line

represents recursive approach’s time complexity.

The graph shows that initially iterative and recursive approaches behave more like

similarly for the values of n but for the greater values of n recursive approach takes more time

than iterative and explicit approach. As its time complexity is O (2n) exponential.
13

Moreover Iterative function take less time than recursive approach but it is not as

efficient as it has linear time complexity and explicit approach has constant time complexity as

shown in figure 3.2.

Time complexity of Fibonacci Series using different methods

Fig (3.2)
14

Study of Time Complexity for sum of n natural numbers

4. Abstract
This report deals with algorithmic complexity used in the determination of sum of n natural
numbers. While exposing three correct algorithms, we have, in the light of complexity study of
each one of them, released the one that is optimal.

5. Methods of Computation

There are mainly three methods to compute sum of n natural numbers. Each section of this report
will describe one method (and closely related methods) of computing the sum of n natural
numbers.

The following algorithms in this report have been seen in print:

 Recursive approach in section 5.1,


 Repeated addition or iteration in section 5.2,
 Explicit form in section 5.3

5.1 Recursive method

int add (int n) ____________________________________ T(n)


{
if (n! = 0) ____________________________________ 1

return n + add (n - 1); ____________________ 1+ T(n-1)

return 0;
}

Time complexity
T(n) = T(n-1) + 1

BIG-O
O (n)
15

Graph and Table


Following fig 5.1 will show the execution of recursive program of sum of n natural numbers for
different values of n. The time is noted in microseconds and three different readings were taken.
Average of three readings show the actual Time complexity of this approach. The graph
represents three readings as well as the average of the recursive time complexity of sum of n
natural numbers.

Fig (5.1)
16

5.2 Iterative method

int add (int n) ____________________________________ T(n)


{
for (int i = 1; i <= n; ++i) ____________________ n

sum += i; _______________________________ n
}

Time complexity
T(n) = 2n

BIG-O
O (n)

Graph and Table


Following fig 5.2 will show the execution of iterative program of sum of n natural numbers for
different values of n. The time is noted in microseconds and three different readings were taken.
Average of three readings show the actual Time complexity of this approach. The graph
represents three readings as well as the average of the iterative time complexity of sum of n
natural numbers.
17

Fig (5.2)
18

5.3 Explicit method

int main()
{
clock_t tStart = clock();
int n, sum ;
cout << "Enter a positive integer: ";
cin >> n;

sum = n*(n+1)/2; _____________________________________ n2

cout << "Sum = " << sum;


cout<<"\ntime= " <<(double)(clock()-tStart)/CLOCKS_PER_SEC<<endl;

return 0;
}

Time complexity
T(n) = n2 + n/2

BIG-O
O (n2)

Graph and Table


Following fig 5.3 will show the execution of explicit program of sum of n natural numbers for
different values of n. The time is noted in microseconds and three different readings were taken.
Average of three readings show the actual Time complexity of this approach. The graph
represents three readings as well as the average of the explicit time complexity of sum of n
natural numbers.
19

Fig (5.3)
20

Time Complexity Analysis sum of nth natural number using different methods

You might also like