c++ - 斐波那契 C++ gmp 生成器

标签 c++ fibonacci gmp

你好,我正在尝试对我编写的现有斐波那契生成器实现 gmp。我一直在阅读 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 库的完全菜鸟,任何帮助都会很棒。我正在阅读文档,但我正在努力实现它。

最佳答案

已解决,谢谢 Marc Glisse 为我指明了正确的方向!

我简单地删除了函数 return 并允许函数只返回输出。

#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$ 

关于c++ - 斐波那契 C++ gmp 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26796165/

相关文章:

java - 递归打印斐波那契数列

php - centOS无法安装GMP

php - 从Docker容器向PHP添加扩展

c++ - 在这个节点定义中,为什么我们在结构体定义之后使用 "Node"?

c++ - 多条路径的最短和

c++ - unsigned long long vs unsigned long(可移植性的观点)

c - 我如何让 C 识别未正确安装的库?

c++ - "using namespace std;"有哪些好的替代品?

c++ - 求解斐波那契数列在函数中递归返回void

java - 使用 GUI 计算斐波那契方法