-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFibonacci.cpp
34 lines (34 loc) · 1.11 KB
/
Fibonacci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
//#include<vector>
using namespace std;
class Fibonacci{
public:
static int findFibonacciByIndexIterative(int inputIndex){
int firstElement=0;
int secondElement=1;
int currentFibonacciNumber=0;
//vector<int> fibonacciVector;
//fibonacciVector.push_back(0);
//fibonacciVector.push_back(1);
for(int i=0;i<=inputIndex-2;i++){
currentFibonacciNumber=firstElement+secondElement;
//fibonacciVector.push_back(currentFibonacciNumber);
firstElement = secondElement;
secondElement = currentFibonacciNumber;
}
//return fibonacciVector[inputIndex];
return currentFibonacciNumber;
}
static int findFibonacciByIndexRecursive(int InputIndex){
if(InputIndex<2){
return InputIndex;
}
else{
return findFibonacciByIndexRecursive(InputIndex-1)+findFibonacciByIndexRecursive(InputIndex-2);
}
}
};
int main(){
cout<<Fibonacci::findFibonacciByIndexIterative(10)<<endl;
cout<<Fibonacci::findFibonacciByIndexRecursive(8)<<endl;
}