Fibonacci series in Data Structures and Algorithms
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:
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
4.5 ★ | 2 Vote
You should read it
May be interested
- Algorithm for sharing (divide and conquer)divide and conquer (divide and conquer) is an important method of designing algorithms. the idea of this method is quite simple and very easy to understand.
- What is Data Structure?data structure is a way of storing, organized and systematic data organization so that data can be used effectively.
- Environment settings in Data structuresbecause c and c ++ languages are the languages that almost every university uses to teach, in this chapter i will guide you to install c and c ++ to run the examples in the configuration series. structure and algorithm.
- Array data structurearray (array) is one of the oldest and most important data structures. arrays can store some fixed elements and these elements have the same type. most data structures use arrays to implement algorithms. here are the important concepts related to arrays.
- What is algorithm?algorithms (also known as algorithms - english is algorithms) is a finite set of instructions to be executed in a certain order to get the desired result. in general, the algorithm is independent of programming languages, ie an algorithm can be deployed in many different programming languages.
- Asymptotic analysis in Data Structures and Algorithmsthe asymptotic analysis of an algorithm is a concept that helps us estimate the running time of an algorithm. using asymptotic analysis, we can draw the best conclusions about the best case scenario, the average case, the worst case of an algorithm.