Generating the Sierpinski triangle iteratively in Mathematica?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Sierpinski triangle is a classic fractal built by repeatedly subdividing a triangle and keeping the corner pieces. In Mathematica, an iterative construction is often cleaner than a fully recursive one because you can represent the current generation as a list of triangles and repeatedly transform that list with Nest or NestList.
Represent the Triangle Geometrically
A convenient starting triangle is an equilateral one with explicit coordinates:
Each iteration replaces one triangle with three smaller corner triangles. The middle triangle is omitted, which is exactly what creates the Sierpinski pattern.
The Iterative Transformation
For a triangle with vertices a, b, and c, compute the midpoints of each side:
- '
(a + b)/2' - '
(b + c)/2' - '
(c + a)/2'
Then keep these three corner triangles:
- '
{a, (a+b)/2, (c+a)/2}' - '
{(a+b)/2, b, (b+c)/2}' - '
{(c+a)/2, (b+c)/2, c}'
In Mathematica, that becomes:
That function transforms one triangle into three smaller ones.
Build the Fractal Iteratively
Now apply that transformation repeatedly to the current list of triangles.
This is a fully iterative approach. Nest applies the same generation step repeatedly, and the triangle list grows by a factor of three on each iteration.
Why NestList Is Also Useful
If you want to inspect intermediate stages, NestList is even better than Nest because it preserves every generation.
That is useful for teaching, debugging, or animating the fractal's growth.
A Different Iterative Idea: The Chaos Game
Another iterative method is the chaos game. Instead of subdividing polygons directly, you repeatedly move a point halfway toward a randomly chosen triangle vertex.
This also produces the Sierpinski triangle, but from a probabilistic point-cloud process rather than from explicit triangle subdivision.
Why the Iterative Version Is Attractive
An iterative list transformation has a few advantages in Mathematica:
- the generation rule is explicit
- intermediate results are easy to inspect
- '
NestandNestListfit the problem naturally' - polygon rendering stays straightforward
This often feels more "Mathematica-like" than manually managing recursion depth yourself.
Common Pitfalls
The biggest mistake is flattening at the wrong level. Each triangle becomes a list of three triangles, so the result must be flattened by one level, not completely flattened into raw points.
Another mistake is using too many iterations without realizing how quickly the triangle count grows. After n iterations, you have 3^n triangles.
People also sometimes mix the subdivision method and the chaos-game method conceptually. Both generate the same fractal pattern, but the data structures and rendering strategies are very different.
Finally, if the output looks distorted, check aspect ratio and triangle coordinates. A bad initial triangle or stretched plotting settings can hide the expected self-similar structure.
Summary
- An iterative Sierpinski construction can be modeled as repeated transformation of a triangle list.
- '
NestandNestListare natural Mathematica tools for this pattern.' - Each triangle is replaced by three corner triangles formed from side midpoints.
- The chaos game is another iterative way to generate the same fractal.
- Be careful with flattening depth, exponential growth in triangle count, and plotting settings.

