项目Euler 57,运行时错误

时间:2016-06-25 11:42:40

标签: c runtime-error continued-fractions

我目前正在Euler项目的Probelm 57工作:

https://projecteuler.net/problem=57

我的问题是,虽然我相信正确的答案正在给出数字的计数似乎是不正确的。我不确定错误可能是什么。我已经包含了评论,希望能让我的代码更清晰。

我的代码正确地识别了1393/985分数但是在大约i = 30之后它似乎变成干线(可能在某处溢出?)。

提前致谢!

P.S。显然答案是153而我得到了253

#include <stdio.h>
long unsigned int  fraction(long unsigned int  *x, long unsigned int  *y);

int main(){
    int i, j, count=0; //initialise loop counters

    for (i=0; i<1000; i++){ //number of iterations
        long unsigned int  x=1, y=2; //this is the starting position values
        for (j=1; j<i; j++){ //iterate through "fraction" function i-1 times
            fraction(&x, &y);
        }
        x+=y; //add the extra "1" onto the final answer
        long unsigned int xt=0, t1=x, t2=y, yt=0; //xt and yt are digit counters
        while(t1!=0){ //count the number of digits in the numerator
          t1 /= 10;             
          xt++;
      }
      while(t2!=0){ //count the number of digits in the denominator
          t2 /= 10;             
          yt++;
      }
      if (xt>yt){ //compare length of numerator and denominator
        count++;
      }
    }
    printf("Count is %i\n",count);
}

long unsigned int  fraction(long unsigned int  *x, long unsigned int  *y){ //function to derive a fraction based on the number of iterations
    long unsigned int  temp;

    *x+=2*(*y); 
    temp=*x;
    *x=*y;
    *y=temp; //modify pointers to reflect new values
}   

0 个答案:

没有答案