fibonacci c ++ gmp generator

时间:2014-11-07 07:27:12

标签: c++ fibonacci gmp

你好我试图将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图书馆的完整菜鸟,任何帮助都会很棒。我正在阅读文档,但我正在努力实现它。

1 个答案:

答案 0 :(得分:1)

解决了,谢谢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$