Skip to content

Instantly share code, notes, and snippets.

@nicolasfig
Last active February 25, 2018 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolasfig/37795cbf21c04e0fc37ffa407b6ab231 to your computer and use it in GitHub Desktop.
Save nicolasfig/37795cbf21c04e0fc37ffa407b6ab231 to your computer and use it in GitHub Desktop.
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