Sunday, 15 February 2015

fibonacci c++ gmp generator -



fibonacci c++ gmp generator -

hello attempting implement gmp existing fibonacci generator wrote. have been reading on gmp documentation there still much not understand. original fibonacci generator here:

#include <iostream> using namespace std; class fib { int n; long unsigned int first, second; public: fib() { first = 0; sec = 1; cout << "enter number of terms of fibonacci series want" << endl; cin >> n; cout << "first " << n << " terms of fibonacci series are:" << endl; } int solve() { int i; long unsigned int next; for(i = 0 ; < n + 1 ; i++) { if(i <= 1) { next = i; } else { next = first + second; first = second; sec = next; } } homecoming next; } }; int main() { fib fib; cout << fib.solve() << endl; homecoming 0; }

i installed gmp using:

sudo apt-get install libgmp3-dev

when seek implement gmp did this:

#include <iostream> #include <gmpxx.h> using namespace std; class fib { int n; mpz_class first, second; public: fib() { first = 0; sec = 1; cout << "enter number of terms of fibonacci series want" << endl; cin >> n; cout << "first " << n << " terms of fibonacci series are:" << endl; } int solve() { int i; mpz_class next; for(i = 0 ; < n + 1 ; i++) { if(i <= 1) { next = i; } else { next = first + second; first = second; sec = next; } } homecoming next; } }; int main() { fib fib; cout << fib.solve() << endl; homecoming 0; }

i know @ point need convert int string, clear output variable or that. when effort compile run:

g++ -lgmpxx -lgmp fib.cpp -o fib

my output:

fib.cpp: in fellow member function ‘int fib::solve()’: fib.cpp:30:12: error: cannot convert ‘mpz_class {aka __gmp_expr<__mpz_struct [1], __mpz_struct [1]>}’ ‘int’ in homecoming homecoming next; ^

i finish noob bignum libraries, help great. reading documentation struggling implementing it.

solved, give thanks marc glisse pointing me in right direction!

i removed function homecoming , allowed function homecoming ouput.

#include <iostream> #include <gmpxx.h> using namespace std; class fib { int n; public: fib() { cout << "enter number of terms of fibonacci series want" << endl; cin >> n; cout << "the " << n << "'st fibonacci number is:" << endl; } void solve() { int i; mpz_class first, second, next; first = 0; sec = 1; for(i = 0 ; < n + 1 ; i++) { if(i <= 1) { next = i; } else { next = first + second; first = second; sec = next; } } cout << next << endl; } }; int main() { fib fib; fib.solve(); homecoming 0; }

output:

enter number of terms of fibonacci series want 3301 first 3301 terms of fibonacci series are: 330153163507162264637094778670152653434758914922281728912670042596222213549775330156165336158736310556035302724174567603559968964146698655928480718496410717009709564103992213321320869628734803460669663152332798570186240768164370808688660485835985642189726235311578136722218902035069558368032277843436948382319806290480685283349217035498351102885889468646619750569482644246863804467015344937199892515242806415403581786532923017170033416624774209919795051514102027827396052441847160310846646083321110222356075543424672128051593137886359425865994528848747739182600228659941846983982384323813903695048726976986370288741982958687841091743740983161275336114608885705665822704734020694899622487801 ubuntu@ubuntu:~/projects/c++/fibonacci_cpp$

c++ fibonacci gmp

No comments:

Post a Comment