Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package recursion;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Recursion {
public static void pascalArray(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
for (int i = 2; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (j == 0) {
matrix[i][j] = 1;
break;
} else if (j == i) {
matrix[i][j] = 1;
} else {
matrix[i][j] = matrix[i - 1][j - 1] + matrix[i - 1][j];
}
}
}
}
public static void main(String[] args) throws IOException {
int size = 20;
int[][] pascalTest = new int[size][size];
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
pascalArray(pascalTest);
for (int i = 2; i < pascalTest.length; i++) {
for (int j = 2; j < pascalTest[0].length; j++) {
writer.write(pascalTest[i][j] + " ");
if (j == i) {
break;
}
}
writer.write("\n");
}
writer.flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.