I tried to draw a Sierpinski triangle by shading the even number entries of the Pascal triangle. Could you please suggest improvements?
public class Sierpinski {
public static void main(String[] args) {
int no_of_row = 50;
int[][] tri = new int[no_of_row][no_of_row];
for (int i = 0; i < no_of_row; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
tri[i][j] = 1;
}
if (i > 0 && j > 0 ) {
tri[i][j] = tri[i - 1][j - 1] + tri[i - 1][j];
}
}
}
for (int i = 0; i < no_of_row; i++) {
printSpace(no_of_row, i);
for (int j = 0; j <= i; j++) {
System.out.print(isEven(tri[i][j]));
System.out.print(" ");
}
System.out.println();
}
}
private static void printSpace(int no_of_row, int current_row) {
for (int i = 0; i < no_of_row - current_row; i++) {
System.out.print(" ");
}
}
private static String isEven(int n) {
return n % 2 == 0 ? "x" : " ";
}
}
Sample output:
x
x x x
x x
x x x
x x x x x x x
x x x x x x
x x x x x x x
x x x x
x x x x x x x x x
x x x x x x
x x x x x x x
x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x
x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x
x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x