Skip to content

Instantly share code, notes, and snippets.

@jnegre
Created December 19, 2011 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnegre/1498493 to your computer and use it in GitHub Desktop.
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
▲ ▲
▲ ▲
▲ ▲ ▲ ▲
▲ ▲
▲ ▲ ▲ ▲
▲ ▲ ▲ ▲
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
▲ ▲
▲ ▲ ▲ ▲
▲ ▲ ▲ ▲
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
▲ ▲ ▲ ▲
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
// 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