Last active
February 25, 2018 18:48
-
-
Save nicolasfig/37795cbf21c04e0fc37ffa407b6ab231 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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