Created
November 10, 2015 18:05
Sierpinski Triangle from Pascal Triangle
This file contains hidden or 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
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