Skip to content

Instantly share code, notes, and snippets.

@javiermontenegro
Last active November 30, 2019 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javiermontenegro/70d8dcfbe6f78aaf14bad259361560ef to your computer and use it in GitHub Desktop.
Save javiermontenegro/70d8dcfbe6f78aaf14bad259361560ef to your computer and use it in GitHub Desktop.
This gist is a example of fibonacci algorithm in C++
/*********************************************************************
* Filename: Cpp_Fibonacci.Number.cpp
* Author: Javier Montenegro (javiermontenegro.github.io)
* Copyright: @2019
* Details: this gist is a example of fibonacci algorithm in C++
*********************************************************************/
#include <iostream>
#include<cstdio>
using namespace std;
const long long MAX = 93;
long long f[MAX] = {0};
long long fn(long long n);
long long fn(long long n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
if (f[n])
return f[n];
long long k = (n%2!=0)? (n+1)/2 : n/2;
f[n] = (n%2!=0)? (fn(k)*fn(k) + fn(k-1)*fn(k-1))
: (2*fn(k-1) + fn(k))*fn(k);
return f[n];
}//End fn
int main(int argc, char **argv)
{
int number;
cout << "Enter a number:";
cin >> number;
cout << "The fibonacci number is :" << fn(number) << endl;
return 0;
}//End main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment