How does it work?
The Sierpinski carpet begins with a square. This square is divided into nine equal parts. The smallest smaller square is removed from the original larger square. Then the remaining squares are again divided into nine equal parts, and the most central square from each square is removed. When this process is repeated, a beautiful Sierpinski carpet pattern is observed.
Suppose we start with a black square.
Divide it into 9 equal parts and remove the central square.
Repeating the process further leads to something like this.
We can visualize this phenomenon in detail in this video .
Let`s see how its code looks like:
This is a Sierpinski carpet after 7 repetitions. You can get its code for other languages at rosettacode .