私は現在、Project Euler の Probelm 57 で作業しています。
https://projecteuler.net/problem=57
私の問題は、正しい答えが与えられていると信じていますが、桁数のカウントが正しくないように見えることです。エラーが何であるかはわかりません。コードをより明確にするために、コメントを含めました。
私のコードは 1393/985 の分数を正しく認識しますが、約 i=30 を過ぎると、うまくいかないようです (どこかでオーバーフローする可能性がありますか?)。
前もって感謝します!
PSどうやら答えは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
}