Mathematica
Sierpinski Triangle
Iterative Methods
Fractal Geometry
Computational Mathematics

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:

wolfram
triangle = {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}};

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:

wolfram
1subdivide[{a_, b_, c_}] := Module[
2  {ab, bc, ca},
3  ab = (a + b)/2;
4  bc = (b + c)/2;
5  ca = (c + a)/2;
6  {
7    {a, ab, ca},
8    {ab, b, bc},
9    {ca, bc, c}
10  }
11]

That function transforms one triangle into three smaller ones.

Build the Fractal Iteratively

Now apply that transformation repeatedly to the current list of triangles.

wolfram
1nextGeneration[triangles_] := Flatten[subdivide /@ triangles, 1]
2
3triangle = {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}};
4triangles = Nest[nextGeneration, {triangle}, 5];
5
6Graphics[
7  Polygon /@ triangles,
8  PlotRange -> All,
9  ImageSize -> Large,
10  Background -> White
11]

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.

wolfram
1generations = NestList[nextGeneration, {triangle}, 4];
2
3Row[
4  Graphics[
5      Polygon /@ #,
6      PlotRange -> All,
7      ImageSize -> 180
8    ] & /@ generations
9]

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.

wolfram
1vertices = {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}};
2
3points = Rest @ NestList[
4   ( # + RandomChoice[vertices] )/2 &,
5   {0.2, 0.1},
6   20000
7];
8
9ListPlot[
10  points,
11  AspectRatio -> Automatic,
12  PlotStyle -> PointSize[Tiny],
13  Axes -> False,
14  ImageSize -> Large
15]

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
  • 'Nest and NestList fit 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.
  • 'Nest and NestList are 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.

Course illustration
Course illustration

All Rights Reserved.