Skip to content

Instantly share code, notes, and snippets.

@bsenduran
Created November 10, 2015 18:05
Show Gist options
  • Save bsenduran/6258498bbbf343cda549 to your computer and use it in GitHub Desktop.
Save bsenduran/6258498bbbf343cda549 to your computer and use it in GitHub Desktop.
Sierpinski Triangle from Pascal Triangle
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" : " ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment