fibonacci c++ gmp generator

fibonacci c++ gmp generator

本文关键字:generator gmp c++ fibonacci      更新时间:2023-10-16

你好,我正在尝试将gmp实现到我编写的现有fibonacci生成器中。我一直在仔细阅读gmp文档,但仍然有很多我不理解的地方。最初的斐波那契生成器在这里:

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

我安装gmp使用:

sudo apt-get-install libgmp3-dev

当我尝试实现gmp时,我这样做了:

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

我知道在某个时候我需要将int转换为字符串,然后清除输出变量或类似的东西。当我尝试编译时,我运行:

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

我的输出:

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

我是bignum库的一个完全的noob,任何帮助都将是伟大的。我正在阅读文档,但我正在努力实现它。

Solved,感谢Marc Glisse为我指明了正确的方向!

我只是删除了函数return,并允许函数只返回outout。

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

输出:

Enter the number of terms of Fibonacci series you want
3301
First 3301 terms of Fibonacci series are:
    330153163507162264637094778670152653434758914922281728912670042596222213549775330156165336158736310556035302724174567603559968964146698655928480718496410717009709564103992213321320869628734803460669663152332798570186240768164370808688660485835985642189726235311578136722218902035069558368032277843436948382319806290480685283349217035498351102885889468646619750569482644246863804467015344937199892515242806415403581786532923017170033416624774209919795051514102027827396052441847160310846646083321110222356075543424672128051593137886359425865994528848747739182600228659941846983982384323813903695048726976986370288741982958687841091743740983161275336114608885705665822704734020694899622487801
ubuntu@ubuntu:~/projects/c++/fibonacci_cpp$