Sierpinski's Triangle

Programming Sierpinski’s triangle.

Sierpinski’s triangle—also known as the Sierpinski gasket—is a fractal which presents a pattern of nested triangles. The Macromedia Flash animation shown here is a real-time calculation and rendering of the first rows of Sierpinski’s triangle. The animation starts off with a single black cell, which is represented in code by a 1, whereas a white cell is represented as a 0. From then on, a cell is black if the cell directly over it and the cell over it at its left are different, which in code translate into a bitwise xor operation (the ^ operator in ActionScript).

Macromedia Flash player required.

It turns out that by representing the odd numbers of Pascal’s triangles as 1s and the even numbers as 0s, one gets Sierpinski’s triangle. Through some thinking, one easily realizes that changing a single character in the code that calculates Pascal’s triangle will create a code that calculates Sierpinski’s triangle. Indeed, to calculate Pascal’s triangle one starts off with a 1, and from then on the value at a certain position is equal to the sum of the values at the position directly over and over it at its left. Therefore, changing a plus sign to a bitwise xor sign yields a code that calculates Sierpinski’s triangle.

The method illustrated here is only one of many methods through which we can obtain Sierpinski’s triangle.

Plaintext source of this program, released under the GPL.