Skip to content

Commit

Permalink
Merge pull request #43 from its-me-Harsh-Anand/main
Browse files Browse the repository at this point in the history
fibonacci number fast algorithm
  • Loading branch information
Meghwantsingh authored Oct 31, 2022
2 parents 55b8fb8 + 2a9df2b commit 43bddad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Binary file added Programs- Coder/C++/Fibonacci series/a.exe
Binary file not shown.
27 changes: 27 additions & 0 deletions Programs- Coder/C++/Fibonacci series/fibonacci_optimised.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
int fibonacci_naive(int n) {
if (n <= 1)
return n;

return fibonacci_naive(n - 1) + fibonacci_naive(n - 2);
}

int fibonacci_fast(int n) {
int prev = 0, next = 1, ans;
if(n==0) return prev;
for(int i=2; i<=n ; i++){
ans = prev + next;
prev = next;
next = ans;
}

return next;
}

int main() {
int n = 0;
cin >> n;
cout << fibonacci_fast(n) << '\n';
return 0;
}

0 comments on commit 43bddad

Please sign in to comment.