1

I need a code for making a Pascal's triangle. This code is for a Right triangle, but I need it to be a Pascal's triangle. It needs to have 10 rows and does have a gap in the middle of the Top and the Bottom. Can anyone please help me on this? for loops will be fine.

public static int get_pascal(int row, int col) {
    if (col == 0 || col == row) {
        return 1;
    } else {
        return get_pascal(row - 1, col - 1) + get_pascal(row - 1, col);
    }
}

public static void main(String[] args) {
    //row size variable
    int rowNum = 5;

    levels = new String[rowNum];

    int i = 0;
    int arIndex = 0;
    System.out.println(recurseRow(i, rowNum, arIndex));
    System.out.println("                                 ");
    System.out.println(upsideDown(rowNum - 1));
}

//Recursion for row
public static String recurseRow(int i, int rowNum, int arrayIndex) {
    if (i == rowNum)
        return "";
    else {
        int k = 0;
        int next = i + 1;
        String str = recurseCol(i, k);
        levels[arrayIndex] = str;
        arrayIndex += 1;
        return str + "\n" + recurseRow(next, rowNum, arrayIndex);
    }
}

//Recursion for column
public static String recurseCol(int i, int k) {
    if (k > i)
        return "";
    else {
        int next = k + 1;
        return get_pascal(i, k) + " " + recurseCol(i, next);
    }
}

//upside down recursion
public static String upsideDown(int index) {
    if (index < 0) {
        return "";
    } else {
        String str = levels[index];
        index -= 1;
        return str + "\n" + upsideDown(index);
    }
}
J_o_a_T
  • 11
  • 4

2 Answers2

1

Pascal's triangle - is a triangular array of binomial coefficients where the elements of the first row and column are equal to one, and all other elements are 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 an iterative method to populate such an array:

public static int[][] pascalsTriangle(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 void main(String[] args) {
    int n = 10;
    System.out.println("n = " + n);
    System.out.println("Pascal's triangle:");
    int[][] arr = pascalsTriangle(n);
    for (int[] row : arr) {
        for (int element : row)
            System.out.printf("%2d ", element);
        System.out.println();
    }
}

Output:

n = 10
Pascal's 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 

See also: Array of binomial coefficients

0

You can add prefix of multiple spaces where you recursively create a row: instead of

String str = recurseCol(i, k);

you'll have

String str = "";
for (int spaces = 0; spaces < 2 * (rowNum - i - 1); spaces++) { 
  str += " ";
}
str += recurseCol(i, k);

You also need to format all numbers to have the same width inside recurseCol, for example now all numbers will be 3 digits wide:

return String.format("%3d %s", get_pascal(i, k), recurseCol(i, next));

The resulting code of the modified methods:

//Recursion for row
public static String recurseRow(int i, int rowNum, int arrayIndex) {
    if( i == rowNum)
        return "";
    else {
        int k = 0;
        int next = i + 1;
  String str = "";
  for (int spaces = 0; spaces < 2 * (rowNum - i - 1); spaces++) {
      str += " ";
  }
        str += recurseCol(i, k);
        levels[arrayIndex] = str;
        arrayIndex += 1;
        return  str + "\n" + recurseRow(next, rowNum, arrayIndex);
    }
}

//Recursion for column
public static String recurseCol(int i, int k) {
    if(k > i)
        return "";
    else {
        int next = k + 1;
        return String.format("%3d %s", get_pascal(i, k), recurseCol(i, next));
    }
}

The "magic" numbers 2 and 3 are related to each other: if each number is n-digits wide, then we need to add a prefix to the string consisting of n-1 spaces repeated rowNum - i - 1 times.

There are other methods to achieve these, but I think that the above allows to get the result with minimum modifications.

Community
  • 1
  • 1
Alex Sveshnikov
  • 4,214
  • 1
  • 10
  • 26