0

I'm trying to develop a program that prints out Pascal's Triangle using recursion. Here are my codes:

public class PascalTriangle {
    public static int[] computePT(int k) {
        int[] pt = new int[k + 1];
        if (k == 0) {
            pt[0] = 1;
            return pt;
        } else {
            int[] ppt = computePT(k - 1);
            pt[0] = pt[k] = 1;
            for (int i = 1; i < ppt.length; i++) {
                pt[i] = ppt[i - 1] + ppt[i];
            }
        }
        return pt;
    }
}
public class PascalTriangleDriver {
    public static void main(String args[]) {
        int k = 10;

        int arr[] = PascalTriangle.computePT(k);

        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + " ");

        System.out.println();
    }
}

The code runs perfectly, however my issue is that I want to modify my PascalTriangle code (not the PascalTriangleDriver code) such that when k=10, for example, it prints out:

1 9 36 84 126 126 84 36 9 1

instead of:

1 10 45 120 210 252 210 120 45 10 1
Kira
  • 205
  • 2
  • 11

2 Answers2

2

You seem to have made an off-by-1 error. One simple way to solve this is to write another method that calls your original method with k-1:

// this is your original method, just renamed:
private static int[] computePTImpl(int k) {
    int[] pt = new int[k + 1];
    if (k == 0) {
        pt[0] = 1;
        return pt;
    } else {
        int[] ppt = computePT(k - 1);
        pt[0] = pt[k] = 1;
        for (int i = 1; i < ppt.length; i++) {
            pt[i] = ppt[i - 1] + ppt[i];
        }
    }
    return pt;
}

// you will call this method:
public static int[] computePT(int k) {
    return computePT(k - 1);
}

Alternatively, you can actually fix your code by replacing ks with k-1s:

public static int[] computePT(int k) {
    int[] pt = new int[k]; // note the change
    if (k == 1) { // note the change
        pt[0] = 1;
        return pt;
    } else {
        int[] ppt = computePT(k - 1);
        pt[0] = pt[k - 1] = 1; // note the change
        for (int i = 1; i < ppt.length; i++) {
            pt[i] = ppt[i - 1] + ppt[i];
        }
    }
    return pt;
}

Note that we don't change the recursive call because if we did, we would be saying that the k-th row of Pascal's triangle depends on the k-2-th row, which is not true.

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

You can iteratively populate an array of binomial coefficients as follows: the first row and column are filled with ones, and all other elements are equal to the sum of the previous element in the row and column.

T[i][j] = T[i][j-1] + T[i-1][j];

You can create two methods: one returns a 2d array containing a triangle, and the second returns the base of that triangle. It is more useful for clarity.

Output:

Triangle:
 1  1  1  1  1  1  1  1  1  1 
 1  2  3  4  5  6  7  8  9 
 1  3  6 10 15 21 28 36 
 1  4 10 20 35 56 84 
 1  5 15 35 70 126 
 1  6 21 56 126 
 1  7 28 84 
 1  8 36 
 1  9 
 1 
Base:
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

Code:

public static void main(String[] args) {
    int n = 10;
    System.out.println("Triangle:");
    int[][] arr = binomialTriangle(n);
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++)
            if (arr[i][j] > 0)
                System.out.printf("%2d ", arr[i][j]);
        System.out.println();
    }

    int[] base = binomial(arr);
    System.out.println("Base:");
    System.out.println(Arrays.toString(base));
}
public static int[][] binomialTriangle(int n) {
    // an array of 'n' rows
    int[][] arr = new int[n][];
    // iterate over the rows of the array
    for (int i = 0; i < n; i++) {
        // a row of 'n-i' elements
        arr[i] = new int[n - i];
        // iterate over the elements of the row
        for (int j = 0; j < n - i; j++) {
            if (i == 0 || j == 0) {
                // elements of the first row
                // and column are equal to one
                arr[i][j] = 1;
            } else {
                // all other elements are the sum of the
                // previous element in the row and column
                arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
            }
        }
    }
    return arr;
}
public static int[] binomial(int[][] arr) {
    int[] base = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        // the last element in the row
        base[i] = arr[i][arr[i].length - 1];
    }
    return base;
}

See also: Finding trinomial coefficients using dynamic programming