Trusted answers to developer questions

What is the Fibonacci Sequence?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

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;
}
}

RELATED TAGS

Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?