What is the Fibonacci Sequence?

Fibonacci numbers, commonly denoted as F(n), form a sequence called the Fibonacci Sequence. Each number in the Fibonacci series is the sum of the two preceding ones, starting from 0 and 1. The formula to compute the sequence is as follows:

Fn=Fn1+Fn2F_{n}=F_{n-1}+F_{n-2}

The slideshow below illustrates the concept:

1 of 6

From a coding perspective, an iterative approach to solving the Fibonacci sequence is given below:

int main(){
int num = 8;
int element1 = 0, element2 = 1, next = 0;
for (int i = 1 ; i < num ; i++ )
{
if ( i <= 1 )
next = i;
else{
next = element1 + element2;
element1 = element2;
element2 = next;
}
cout << "Adding " << element1 << " and " << next << " = " << element2+element1 << endl;
}
}

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved