Created
December 19, 2011 19:22
-
-
Save jnegre/1498493 to your computer and use it in GitHub Desktop.
Constructs a Sierpinski triangle in a Unicode-aware console with fixed-width font
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
▲ | |
▲ ▲ | |
▲ ▲ | |
▲ ▲ ▲ ▲ | |
▲ ▲ | |
▲ ▲ ▲ ▲ | |
▲ ▲ ▲ ▲ | |
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ | |
▲ ▲ | |
▲ ▲ ▲ ▲ | |
▲ ▲ ▲ ▲ | |
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ | |
▲ ▲ ▲ ▲ | |
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ | |
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ | |
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ |
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
// Constructs a Sierpinski triangle in a Unicode-aware console with fixed-width font | |
// Based on an idea from Olivier Croisier http://twitter.com/OlivierCroisier/statuses/148809114531799040 | |
int numOfLines = 16 //2^n | |
int[][] c = new int[numOfLines][numOfLines] | |
int delta = numOfLines | |
c[0][0] = 1 | |
println "${' '*delta--}\u25B2" | |
(1..<numOfLines).each { i-> | |
c[i][0] = 1 | |
print "${' '*delta--}\u25B2 " | |
(1..<i).each { j -> | |
c[i][j] = c[i-1][j-1] + c[i-1][j] | |
print ((c[i][j]%2)?'\u25B2 ':' ') | |
} | |
c[i][i] = 1 | |
println '\u25B2 ' | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment