C ++中的Fibonacci系列不能超过47个数字

时间:2014-03-04 23:16:34

标签: c++ fibonacci

我设计的这个程序可以打印斐波那契系列(series[i] = series[i-1] + series[i-2]),但是我不能得到超过47个数字,因为第48个它们变成了负数和奇数(我认为当列表出现时会发生这种情况)范围或项目为空):

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
    int length;
    string again = "";
    do {
        cout << "Enter the length you want in your sequence: ";
        cin >> length;
        vector<int> series(length);
        for (int n=0; n<=1; n++) series[n] = n;
        for (int number=2; number<=length; number++) {
            series[number] = series[number-1] + series[number-2];
        }
        for (int i=0; i<length; i++) cout << series[i] << " ";
        cout << endl << "Do it again ? <y/n> ";
        cin >> again;
        cout << endl;
    } while (again == "y");
}

修改

“改进”代码:

#include <iostream>
#include <vector>
#include <string>

std::vector<int> fibonacci (int length)
{
    std::vector<int> series(length);
    series[0] = 0;
    series[1] = 1;
    for (int num=2; num<length; num++) {
        series[num] = series[num-1] + series[num-2];
    }
    return series;
}

int main ()
{
    std::string again;
    do {
        std::cout << "Enter how many numbers you want in your series: ";
        int length;
        std::cin >> length;
        std::vector<int> series(length);
        series = fibonacci(length);
        for (int n=0; n<length; n++) std::cout << series[n] << " ";
        std::cout << "\nDo it again <y/n> ? ";
        std::cin >> again;
        std::cout << std::endl;
    } while (again == "y");
}

3 个答案:

答案 0 :(得分:7)

当你达到第47个值时,数字超出int范围。最大int值为2,147,483,647,第46个数字位于1,836,311,903以下。第47个数字超过2,971,215,073的最大值。

另外,正如LeonardBlunderbuss所提到的,你超出了你拥有for循环的向量范围。向量以0开头,因此通过number<=length;将调用range+1元素。范围最多只有length-1

答案 1 :(得分:4)

您遇到整数溢出,这意味着您正在尝试计算超出INT_MAXINT_MIN范围的数字。在无符号数的情况下,它只是溢出到零并重新开始,而在有符号整数的情况下,它会翻转到INT_MIN。在这两种情况下,这都称为整数溢出或整数环绕。

您可以使用long long int(在大多数现代系统上可能为64位)而不是int为原始数据类型添加创可贴,或者您可以使用更好的方法就像一个支持(几乎)任意长数据类型的库,如libBigInteger

<强>参考

  1. 整数溢出,已访问2014-03-04,<http://en.wikipedia.org/wiki/Integer_overflow>
  2. C ++ Big Integer Library ,已访问2014-03-04,<https://mattmccutchen.net/bigint/>
  3. limits.h头文件,已访问2014-03-04,<http://tigcc.ticalc.org/doc/limits.html>

答案 2 :(得分:0)

这是我计算BIG斐波纳契数的解决方案

// Study for algorithm that counts n:th fibonacci number                        

#include <iostream>
#include <cstdlib>

#include "boost/multiprecision/cpp_int.hpp"

#define get_buffer(a) buffer[(a)%2]
#define BIG boost::multiprecision::cpp_int

int main(int argc, const char* argv[])
{
  // atoi returns 0 if not integer                                              
  if(argc != 2 || atoi(argv[1]) < 1){
    std::cout << "You must provide one argument. Integer > 0" << std::endl;
    return EXIT_SUCCESS;
  }

  // ring buffer to store previous two fibonacci number, index it with  [i%2]   
  // use defined function get_buffer(i), it will do the magic for you           
  BIG buffer[2]={ 1, 1 };

  // n:th Fibonacci                                                             
  unsigned int fn = atoi(argv[1]);

  // count loop is used if seeked fibonacci number is gt 2                      
  if(fn > 2){
    for(unsigned int i = 2; i < fn; ++i){                                       
      get_buffer(i) = get_buffer(i-1) + get_buffer(i-2);
      // get_buffer(i-1) + get_buffer(i-2) == buffer[0] + buffer[1]
      // if you want to print out every result, do it here
    }
  }

  // Result will be send to cout                                                
  std::cout << "Fibonacci[" << fn << "] is " << get_buffer(fn-1) << std::endl;
  return EXIT_SUCCESS;
}