2014-09-28 65 views
1

我正在研究欧拉项目Problem 14,我需要找到1,000,000下最长的collat​​z序列。我想出了一种适用于较小数字(比如100)的算法,该算法将每个1到100之间的collat​​z数存储到一个数组中,并将该数组用作参考,以加快计算数量。 我的代码如下:分割错误C++(数组太大?)

#include <iostream> 
using namespace std; 

long even(long n){ //the even-collatz function 
    n=n/2; 
    return n; 
} 

long odd(long n){ //the odd collatz function 
    n=3*n+1; 
    return n; 
} 

int main(){ 
    long x, c=0, y[1000000]; // x= the number we are finding the collatz number of, c a counter that keeps track of how many steps we've taken in the sequence, y is an array to store the collatz numbers. 

    for (x=1; x<1000000; x++){ //iterates from x=1 to 1 million 
      long a = x;  //sets a number a equal to the number we are currently trying to find the collatz number of 
      long b = a;  
      c=0;     //intializes counter at 0 
      while (a!=0){   //loops infinitely; the only way to exit is through a break. 
        if (a%2==0){ // detects if the number is even 
          a=even(a);  //applies the even-collatz function if so; sets x=x/2 
          c=c+1; 
          if (y[a]!=0){ // checks if the collatz number of x is already discovered 
            y[b]=c+y[a]; //adds the current number of steps to the collatz number of x and 
            break; //exits the while loop 
          } 

        } 
        else if (a==1){   //checks if the new x is equal to one and 
          y[b]=c;   //if it is, it writes the current value of c to y[b] and 
          break;   // exits the loop 
        } 
        else if (a%2==1){  //same as the "even" block, except for odd numbers 

          a=odd(a); 
          c=c+1; 
          if(y[a]!=0){ 
            y[b]=c+y[a]; 
            break; 
          } 

        } 
      //this is the end of the while loop; we've applied the collatz function as many times as we've needed to to x, and incremented the counter each time 
      } 
    } 

    long z; 
    for (int n=0;n!=100;n++){ 
      if (y[n+1]>y[n]){ 
        z=y[n+1]; 
      } 
    } 
    cout << z << "\n"; 


} 

我遇到的问题是,我得到一个segfault在x = 1818在for循环。通过调试,我发现段错误发生的速度取决于数组y的大小,所以我假设该数组太大了。从我对segfaults的基本理解来看,我认为我只是访问了我“不被允许”的内存。有什么方法可以绕过这个问题吗?或者我应该开始努力寻找解决这个问题的另一种解决方案吗?我正在Ubuntu Studio上使用g ++进行编译。

回答

5

该数组对于系统的默认堆栈大小可能太大;最简单的解决方法是将其定义更改为:

std::vector<long> y(1000000); 

其他所有内容都可以保持不变。您可以在循环中稍后使用y.size()而不是幻数1000000

-1

如果y对于您的堆栈太大,只要main试图运行,就会发生堆栈溢出异常。

你的问题更可能是ay的大小更大。 当我通过调试器运行时,a是1417174,当x是4255时,所以你可能在算法上有问题。这就是说,你应该自己分配它,或者使它成为静态的,因为不能保证Project Euler使用的任何编译器都会允许这么大的堆栈大小。

+0

堆栈溢出导致许多平台上的分段错误。 – 2014-09-28 00:33:38

+0

@HevyLight是的,但它不依赖于x有多大 - 只要函数运行,您就会得到异常。如果分段错误是由缓冲区溢出导致的,而不是堆栈溢出。 – 2014-09-28 02:00:28

0

对于起始号码N collat​​z序列可以超越N。对于N == 1000000考虑x == 333335

我会建议你做一个yvector<int>动态地扩展它,或者只是让unordered_map<int, int>