斐波那契数列对大数出错 我刚刚开始学习C++,这里是我的C++代码,用来生成斐波那契序列。p> #include <iostream> using namespace std; int main() { int n; while(true){ cout << "Enter the number of terms upto which a the fibonocci sequence should be generated: "; cin >> n; cout << "------------------------" << endl; long long fib1 = 1; long long fib2 = 1; long long fibnext; cout << 1 << " " << fib1 << endl << 2 << " " << fib2 <<endl; for (int i =1; i <= n-2; ++i){ fibnext = fib1 + fib2; fib1 = fib2; fib2 = fibnext; cout << i+2 << " " << fibnext << endl; } } return 0; }
请查看下表中的值: 4660046610375530309 - 92nd Fibonacci number 9223372036854775807 - largest 64-bit two's complement, 2^63 - 1 12200160415121876738 - 93rd Fibonacci number 18446744073709551615 - largest 64-bit unsigned, 2^64 - 1 对于较大的值,可以切换到任意精度(通俗地称为“bignum”)数学库,例如斐波那契数列对大数出错 我刚刚开始学习C++,这里是我的C++代码,用来生成斐波那契序列。p> #include <iostream> using namespace std; int main() { int n; while(true){ cout << "Enter the number of terms upto which a the fibonocci sequence should be generated: "; cin >> n; cout << "------------------------" << endl; long long fib1 = 1; long long fib2 = 1; long long fibnext; cout << 1 << " " << fib1 << endl << 2 << " " << fib2 <<endl; for (int i =1; i <= n-2; ++i){ fibnext = fib1 + fib2; fib1 = fib2; fib2 = fibnext; cout << i+2 << " " << fibnext << endl; } } return 0; },c++,C++,请查看下表中的值: 4660046610375530309 - 92nd Fibonacci number 9223372036854775807 - largest 64-bit two's complement, 2^63 - 1 12200160415121876738 - 93rd Fibonacci number 18446744073709551615 - largest 64-bit unsigned, 2^64 - 1 对于较大的值,可以切换到任意精度(
作为示例,以下是修改后的原始代码,以使用MPIR并输出前千个斐波那契数:
#include <mpir.h>
#include <iostream>
// Helper function for checked output.
void OutZ(int num, mpz_t &x) {
static char textX[10000]; // can handle up to fib(47846)
if (gmp_snprintf(textX, sizeof(textX), "%Zd", x) >= sizeof(textX)) {
std::cout << "*** ERROR: Number was too big, need more space\n";
exit(1);
}
std::cout << num << " " << textX << '\n';
}
int main() {
// Initialise all MPIR integers to specific values.
mpz_t fib1, fib2, fib3;
mpz_init_set_ui(fib1, 1); // fib1 = 1
mpz_init_set_ui(fib2, 1); // fib2 = 1
mpz_init(fib3); // fib3 = 0
// No need for helper, these first two are well-known.
std::cout << "1 1\n2 1\n";
// Now do the rest of them.
for (int i = 3; i <= 1000; ++i) {
mpz_add(fib3, fib1, fib2); // fib3 = fib1 + fib2
mpz_set(fib1, fib2); // fib1 = fib2
mpz_set(fib2, fib3); // fib2 = fib3
OutZ(i, fib3);
}
return 0;
}
您可以通过询问以下问题来验证最终结果:
请查看下表中的值: 4660046610375530309 - 92nd Fibonacci number 9223372036854775807 - largest 64-bit two's complement, 2^63 - 1 12200160415121876738 - 93rd Fibonacci number 18446744073709551615 - largest 64-bit unsigned, 2^64 - 1 对于较大的值,可以切换到任意精度(通俗地称为“bignum”)数学库,例如
作为示例,以下是修改后的原始代码,以使用MPIR并输出前千个斐波那契数:
#include <mpir.h>
#include <iostream>
// Helper function for checked output.
void OutZ(int num, mpz_t &x) {
static char textX[10000]; // can handle up to fib(47846)
if (gmp_snprintf(textX, sizeof(textX), "%Zd", x) >= sizeof(textX)) {
std::cout << "*** ERROR: Number was too big, need more space\n";
exit(1);
}
std::cout << num << " " << textX << '\n';
}
int main() {
// Initialise all MPIR integers to specific values.
mpz_t fib1, fib2, fib3;
mpz_init_set_ui(fib1, 1); // fib1 = 1
mpz_init_set_ui(fib2, 1); // fib2 = 1
mpz_init(fib3); // fib3 = 0
// No need for helper, these first two are well-known.
std::cout << "1 1\n2 1\n";
// Now do the rest of them.
for (int i = 3; i <= 1000; ++i) {
mpz_add(fib3, fib1, fib2); // fib3 = fib1 + fib2
mpz_set(fib1, fib2); // fib1 = fib2
mpz_set(fib2, fib3); // fib2 = fib3
OutZ(i, fib3);
}
return 0;
}
您可以通过询问以下问题来验证最终结果:
这是因为原始整数类型的精度是有限的
long-long
保证至少有64位,但通常不再有。您需要使用某种类型的bignum/bigint实现(最好是一个已经存在的库),以获得无限(而不是任意)精度?您的变量很可能溢出,因为第93个斐波那契数非常大]使用任意精度的数学库:第93个斐波那契数是最大的一个,适合64位,但它需要一个无符号类型。谢谢,我看到了这个GMP库,我将在以后使用它,因为基本整数类型的精度是有限的long-long
保证至少有64位,但通常不再有。您需要使用某种类型的bignum/bigint实现(最好是一个已经存在的库),以获得无限(而不是任意)精度?您的变量很可能溢出,因为第93个fibonacci数非常大]使用任意精度的数学库:第93个fibonacci数是适合64位的最大数,但它需要一个无符号类型。谢谢,我看到了这个GMP库,稍后我将使用它
999 26863810024485359386146727202142923967616609318986952340123175997617981700247881689338369654483356564191827856161443356312976673642210350324634850410377680367334151172899169723197082763985615764450078474174626
1000 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
Result:
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875