Project Euler #33: Digit canceling fractions

  • + 0 comments

    int take_element_4_1 (int a, int b) { int count=0; int res=0; if(b<10) return 0; else if (b>9 && b<100) { res=a-b; if (res%1000 !=0 ) return 0; else return res/1000; } else { while (a) { if (a%10==b%10) { a/=10; b/=10; count ++; } else { res= res*10+a%10;
    a/=10;
    }

         }
        if (count==3) return res; 
        else return 0;
    }
    

    }

    int check_last_case (int a,int b ,int x,int y ) {

     if (x<10 || y<10 ) return 0; 
    else if (x <100 && y<100  ) 
    {
            a=take_element_4_1(a,x) ; 
            b=take_element_4_1(b,y) ;
            if (a==0 || b==0) return 0;
            else return (a==b);
    
    }
    else {
        a=take_element_4_1(a,x); 
        b=take_element_4_1(b,y);
        if (a==0 || b==0) return 0; 
        else if (a>10 || b>10) return 0; 
        else return (a==b);
    }       
    

    }

    main() int i=1;

    while (i <= 998) {
        int j = i + 1;
        if (j >= 10 * i)
            i++;
        else {
            while (j <= 999) {
                if (GCD(i, j) > 1)
                    j++;
                else {
                    int cnt = 1000 / i + 1;
                    while (j * cnt <= 9999) {
                        for (int u = 1; u <= 999 / j; u++) { 
    
                            if (check_last_case(i * cnt, j *cnt, i * u, j * u)) {
                                if (d[i * cnt][j * cnt] == 0) {
                                    numerator += i * cnt;
                                    denominator += j * cnt;
    
                                }
                                d[i * cnt][j * cnt]++;
                            }
                        }
                        cnt++;
                    }
                    j++;
                }
            }
            i++;
        }
    }
    
    cout<<numerator<<" "<<denominator;