Last active
July 3, 2017 02:19
-
-
Save jfacoustic/fb04729919f2fc4e5c567f42ba5546c1 to your computer and use it in GitHub Desktop.
Fibonacci with GMPXX; dynamic and traditional implimentations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <ctime> | |
#include <gmpxx.h> | |
using namespace std; | |
mpz_class fibNorm(int n) { | |
if (n <= 1 ) return n; | |
return fibNorm(n-1) + fibNorm(n-2); | |
} | |
mpz_class fibDyn(int n, vector< mpz_class > & fibList) { | |
if (fibList[n] == -1) { | |
if (n <= 1) | |
fibList[n] = n; | |
else { | |
mpz_class temp1 = fibDyn( n - 1, fibList); | |
mpz_class temp2 = fibDyn( n - 2, fibList); | |
fibList[n] = temp1 + temp2; | |
} | |
} | |
return fibList[n]; | |
} | |
int main() { | |
int n; | |
vector<mpz_class>fibList; | |
cin >> n; | |
for (int i = 0; i <= n; i++) fibList.push_back(mpz_class("-1")); | |
clock_t t1 = clock(); | |
cout << "Dynamic Algorithm " << fibDyn(n, fibList) << " "; | |
clock_t t2 = clock() - t1; | |
cout << "Ticks: " << t2 << endl; | |
t1 = clock(); | |
cout << "Traditional Algorithm " fibNorm(n) << " "; | |
t2 = clock() - t1; | |
cout << "Ticks: " << t2 << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment