Project Euler #148: Exploring Pascal's triangle.

  • + 0 comments

    Got it for N == R. But not getting it right when R < N. Need to work on it. Any clue?

    private static long pascal(int N, int R) { long count = 0; long div71 = 0; // divisible by 7 forming big pattern starting after row 49 long div72 = 0; // divisible by 7 forming small pattern starting after row 7 long div7 = 0; // Divisible by 7 long total = 0; int i = 0;

        long n = N;
        i = 0;
        while (n >= 49) {                
            div71 = div71 + i * (48 * 49)/2;
            div72 = div72 + (i+1) * (6 * 7)/2 * (6 * 7)/2;
            i++;
            n = n - 49;
        }
    
        if(N > 7 || n > 0) {
            if(N >= 49) 
                div71 = div71 + i*(48*49/2) - i*(48-n)*(49-n)/2;
            else if(n > 7) {
                i = 0;
                while(n > 7) {
                    div72 = div72 + (i)*(6*7)/2;
                    n = n - 7;
                    i++;
                }
    
                if(n > 0) {
                    div72 = div72 + (i)*(6*7)/2 - (i)*(6-n)*(7-n)/2;
                }
            }
        }
    
        if(R >= N)
            total = N * (N+1)/2;
        else {
            total = R*(R+1)/2 + (N-R)*R;
        }
    
        div7 = div71 + div72;
    
        count = total - div7;
    
        System.out.println("N: " + N + " R: " + R + " Total: " + total + " Count: " + count);
    
        return count;
    }