fibonacci c++ gmp generator
fibonacci c++ gmp generator
你好,我正在尝试将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$
相关文章:
- 使用 GMP 的 while 循环出现问题
- 有没有一个 c++ gmp 库函数与 python gmpy2 库 divm(..) 函数相同?
- 使用 MinGW 在 Windows 上安装 GMP 时出错
- 为什么gmp会在这里与"invalid next size"重新定位一起崩溃?
- MSYS2 MinGW64 在 Windows 上构建 GMP/MPFR 作为静态库,并将它们链接到使用 CL 编译的
- 尝试运行 gmp 时未找到 MPIR.dll
- GMP-将64位整数存储在mpz_t/mpz_class中,并返回64位整数
- 使用Rcpp返回GMP类
- C++ - 带有 % 运算符的 GMP 库错误
- 如何导入使用自制软件安装的 GMP?
- 为什么提升日志中断会提升 GMP 多精度?
- 使用 GMP/ARB 矩阵减少 OpenMP
- 我可以通过设置工具链文件来替换调用 cmake -G<generator> 选项和调用 vcvarsall.bat吗?
- 使用GMP在C 中编译代码
- 如何使用 GMP 创建浮点数组?
- gmp 的 libgmp.so 有哪些依赖关系?我不断收到未定义的引用
- 如何使用整数初始化 GMP mpz_t
- 如何使用 std::cout 和 mini-gmp mpz-t.
- Visual Studio Linux跨平台的Cmake Generator
- fibonacci c++ gmp generator