0

I'm currently working in Probelm 57 in Project Euler:

https://projecteuler.net/problem=57

My problem is that whilst I believe the correct answers are being given the counting of the digits appears to be incorrect. I am unsure on what the error could possibly be. I have included comments to hopefully make my code clearer.

My code correctly recognises the 1393/985 fraction but after about i=30 it seems to go hay-wire (possibly overflow somewhere?).

Thanks in advance!

P.S. apparently the answer is 153 whilst I get 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
}   
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Tujamo
  • 93
  • 8
  • 2
    The numerator and denominator of the fractions exceed the range of `long unsigned int`. – Martin R Jun 25 '16 at 12:26
  • 1
    On thing useful while doing project Euler problems is to use a *(maybe build your own)* library of big number functions...hint: Theoretically here numbers are treated as arrays – Cherubim Jun 25 '16 at 12:31

0 Answers0