Fibonacci series in Data Structures and Algorithms

The Fibonacci sequence creates numbers by adding two numbers in front. Fibonacci series start from two numbers: F0 & F1. The initial value of F0 & F1 may be 0, 1 or 1, 1, respectively.

What is the Fibonacci sequence?

The Fibonacci sequence creates numbers by adding two numbers in front. Fibonacci series start from two numbers: F0 & F1. The initial value of F0 & F1 may be 0, 1 or 1, 1, respectively.

The condition of the Fibonacci sequence is:

F n = F n-1 + F n-2 

Example of a Fibonacci sequence:

F 8 = 0 1 1 2 3 5 8 13

Example of another Fibonacci sequence:

F 8 = 1 1 2 3 5 8 13 21

Below is an illustration of the Fibonacci sequence on:

Picture 1 of Fibonacci series in Data Structures and Algorithms

The algorithm uses a loop for the Fibonacci sequence

First, our algorithm will use the loop to create the Fibonacci sequence:

 B ắ t  đầ u gi ả i thu ậ t  Fibonacci ( n )  khai b á o f 0 ,  f 1 ,  fib ,  loop  Thi ế t l ậ p f 0  l à 0 Thi ế t l ậ p f 1  l à 1 hi ể n th ị  f 0 ,  f 1 for  loop  ← 1  t ớ i n fib  ←  f 0 +  f 1  f 0 ←  f 1  f 1 ←  fib  hi ể n th ị  d ã y fib  k ế t th ú c  for  K ế t th ú c gi ả i thu ậ t 

The algorithm uses recursion for the Fibonacci sequence

Next, based on recursion we will design the algorithm for the Fibonacci sequence as follows:

 B ắ t  đầ u gi ả i thu ậ t  Fibonacci ( n )  khai b á o f 0 ,  f 1 ,  fib ,  loop  Thi ế t l ậ p f 0  l à 0 Thi ế t l ậ p f 1  l à 1 hi ể n th ị  f 0 ,  f 1 for  loop  ← 1  t ớ i n fib  ←  f 0 +  f 1  f 0 ←  f 1  f 1 ←  fib  hi ể n th ị  d ã y fib  k ế t th ú c  for  K ế t th ú c gi ả i thu ậ t 

According to Tutorialspoint

Previous article: Problem of Hanoi Tower (Tower of Hanoi)

Next article: assert.h in C

« PREV POST
READ NEXT »