1 — Introduction
Imagine a simple creature living on a blank, infinite plane. It follows this one simple instruction: step forward exactly one unit, then turn by a chosen angle. It loops this behavior until we tell it to stop.
The trajectory it traces depends entirely on how much it turns at each step. Tell it to turn 90° every time, and it walks in a square. Tell it to turn 60° every time, and it traces a hexagon. Instruct it to turn by a constant irrational angle, and the path will wander chaotically over a circular orbit forever, never fully repeating a step.
In computer science, this kind of agent is known as a turtle, which is studied under the turtle graphics umbrella. Its origin dates back to the 1960s. Depending on the variant, more commands may be available. For this article, we'll stick to the very restricted command set described here. Note that Mathematica's built-in AnglePath function is similar to our restricted algorithm.
We are going to give our turtle a specific turning rule and draw the paths it traces. We'll use a discrete set of turns by choosing a positive integer S, and dividing a full circle into S equal angular slices, each measuring 360°/S. Our turtle can only face one of these S directions. At step n, it advances its direction index by a certain amount (measured in these 360°/S slices) before stepping forward. We'll try different values of S to see what kind of paths we can draw.
Here is a demo that uses the same rule we'll analyze in this article, for some small values of S. You can move the turtle step-by-step, in S-step blocks, or make it walk automatically.
The gold dot is the walker's position.
Notice that the turtle moves very systematically, and symmetrical patterns begin to emerge. The orientation changes at each step are following some underlying pattern. In later sections of this article, we'll see that many values of S produce highly symmetric rosette-like patterns. We'll try to answer two fundamental questions:
- Will the turtle draw a closed loop-like shape or wander on the plane indefinitely?
- What are the mathematical reasons behind the symmetries that emerges?
2 — The Turning Rule
Let's say we fixed some positive integer S. At step number n ∈ {0, 1, 2, ...}, the turtle turns clockwise by n² slots. We can process this slot number modulo S, because whole multiples of S correspond to no turn at all. The remainder mod S dictates the actual change in direction.
Tracking slots instead of degrees keeps our formulas cleaner. Let's trace a small sequence where S = 6 (providing six equally spaced headings, each 60° apart). At step 0, the turn is 0² = 0. At step 1, it's 1² = 1 slot. At step 2, it leaps to 2² = 4 slots, and so on.
| Step n | Turn n² | Turn mod 6 | Cumulative slot | Direction |
|---|
We could use many other functions to drive our turtle's movements. A linear rule of turning by n slots at step n yields curves resembling the classical Euler spiral. A randomized rule produces the chaotic path of a drunkard's walk. I've found the square function to be a good compromise: it's both very simple to describe and capable of producing beautiful shapes. The goal of this article is to understand why this particular rule produces these patterns.
The cumulative direction slot after step n (starting from n=0) is the sum of the sequence 0² + 1² + 2² + ... + n² which is equal to the following cubic polynomial:
We'll use these two formulas throughout the article to explain the periodicities and symmetries of the paths our turtle draws.
3 — Exposition : Some Large Examples
Before analyzing the math, let's look at some examples with large values of S. Keep in mind: these aren't manually created. Each image is generated directly from the simple turning rule described above. I've selected the S values that I think produce beautiful paths.
Select any S to inspect resulting path in the large viewer. You'll notice that the paths have both rotational and reflectional symmetries. Visually, this creates rosette-like shapes with varying number of "petals". We will spend the remainder of the article investigating how these shapes emerge from our turning rule.
4 — Will the Path Close or Drift?
For most values of S, the turtle's path forms a closed, circular rosette-like symmetric shape. The possible number of "petals" on these rosettes is one of 1, 2, 3, 4, 6, or 12 — which is the set of positive divisors of 12. However, for some specific values of S, the path drifts along the positive or negative x-axis. You can use the interactive sandbox below to see this behavioral difference.
Which values of S produce closed paths, and which produce drifting ones?
The Simple Case: gcd(S, 6) > 1
When the greatest common divisor of S and 6 is greater than 1 (meaning 2, 3, or both divide S), the path will always close into a symmetric rosette-shape and loop periodically over that same shape. The total number of "visible" petals will be either gcd(S, 6) or 2×gcd(S, 6). I'll explain why the visible petal count can appear doubled in a later section.
The Complex Case: gcd(S, 6) = 1
This scenario is far more subtle. Through visual inspection of various values of S, I could identify three behaviors:
- If S is "square-free" (meaning all of its prime factors appear with an exponent of exactly 1), the path drifts. Examples: S = 13, S = 35 (5×7), S = 77 (7×11).
- If all the positive even powered prime factors of S happen to belong to a "special" set of primes, the path also drifts. Example: S = 11×11 = 121 (where 11 is one of those special primes).
- Conversely, if S contains an even powered prime factor that falls outside that special set, the path closes and loops over a 1-petal shape. Example: S = 7×7 = 49.
Through manual experimentation I've found that the first few members of this special set of primes are: {11, 13, 23, 37, 47, 59, 61, 71, 73, …}. I have not yet found a complete characterization of this phenomenon, so I present it here only as an empirical observation. Searching this sub-seuence this in (OEIS) yields a few promising candidates. So that can be a good starting point for a future reasearch on this topic.
5 — Periodicity of the Moves
Let's revisit the sum-of-squares formula: f(n) = n(n+1)(2n+1)/6. It's not hard to guess that the denominator 6 should be responsible for the relation between gcd(S, 6) and graphical properties of the paths we have seen so far. To understand this relation better, let's define two core functions of our turtle's movement.
- Let Δθ(n) = n² mod S represent the relative turn in direction slot at step n.
- Let θ(n) = 1² + 2² + ... + n² mod S = n(n+1)(2n+1) 6 mod S represent the cumulative absolute heading slot of the turtle at step n.
As mentioned before, every slot corresponds to a 1/S fraction of a full 360-degree turn. Therefore, the mod S operation in the definitions of both Δθ(n) and θ(n) makes sense, since any integer multiple of S is identical to making no turn at all.
Δθ(n) is periodic
Our first observation is that the sequence of relative orientation shifts is periodic, cycling every S steps. The arithmetic proof for this is very simple:
This means that after S steps, the turtle arrives at a new coordinate position and heading, but it will receive the same sequence of instructions from that point on in the next S steps. Hence, the turtle will trace an identical shape during the next S steps, simply translated and rotated in space.
This S-periodicity alone explains why we see repeating patterns in the visualizations. However, this doesn't explain why the path closes and loops over the same shape for most values of S, and why every S-step block seems to have internal reflectional symmetry. There are other algebraic properties that cause these two behaviors. The first property is related to the observation below:
θ(n) is periodic
Let's check what can we say about θ(n + S) mod S for all n. We'll expand the sum-of-squares formula to make the analysis easier: n(n + 1)(2n + 1) / 6 = (2n³ + 3n² + n) / 6. Here is a little bit of algebra:
= 2(n³ + 3n²S + 3nS² + S³) + 3(n² + 2nS + S²) + (n + S) 6
= 2n³ + 6n²S + 6nS² + 2S³ + 3n² + 6nS + 3S² + n + S 6
= 2n³ + 3n² + n 6 + 6n²S + 6nS² + 6nS 6 + 2S³ + 3S² + S 6
= θ(n) + S(n² + nS + n) + 2S³ + 3S² + S 6
= θ(n) + 2S³ + 3S² + S 6 mod S
= θ(n) + θ(S) mod S
This fact shows that after every block of S steps, the turtle's absolute orientation is shifted by a constant offset of θ(S) slots. Since θ(S) = 2S³ + 3S² + S 6 = S(S+1)(2S+1) 6 mod S, we can guarantee that the orientation will eventually repeat the same sequence of cumulative turns in no more than 6S steps. We can do better, and calculate the precise minimum cycle required.
To find that minimum cycle, let's dissect θ(S) a bit further. If we factor out the multiplier S, we arrive at θ(S) = S (S+1)(2S+1) 6 mod S . Let's designate the numerator of the fractional part as kS = (S+1)(2S+1). It becomes clear that we only need to evaluate the remainder of kS mod 6. Any whole integer multiples of S will disappear in the modulo S arithmetic. Whatever fraction remains dictates the exact minimum period (counted in S-step blocks). By checking all possible values of S mod 6, we construct the following table:
| S mod 6 | KS | KS mod 6 | θ(S) mod S | Period | GCD(S, 6) |
|---|
The table confirms our earlier visual hypothesis: the cumulative orientation period aligns with GCD(S,6) × S steps.
This periodic behavior mean that the turtle will return to its exact initial heading after k×S steps, where k is 1, 2, 3, or 6. After that point, it will periodically repeat the same sequence of k×S steps. So the turtle will draw the exact same shape, aligned without rotation, over and over. However, the previous caveat still exist: orientation match does not guarantee a return to the starting spatial coordinates. The turtle might be drawing the same shape, but displaced away from the origin by the accumulated vector of its steps. That displacement is what causes the "drifter" paths.
6 — Number of Petals
We have now shown that both the closure status of the path and the number of visual petals are determined by GCD(S, 6). Let's analyze how these two properties affect the graphs.
Closure when GCD(S, 6) = 1
As previewed in the section When Does the Path Close?, this scenario is more complicated. For certain values of S, the path drifts along the positive or negative x-axis. For others, it forms a closed path, and the turtle retraces it forever.
In this case, we know that θ(S) = 0. This guarantees that the heading after S steps is identical to the starting heading. If the spatial coordiantes of the turtle after S steps also happens to match the origin, we achieve a closed, 1-petal shape. If the position is offset, the graph drifts. For the sake of taxonomy, I will classify both behaviors under the "1-petal" name, aligning the petal count directly with the GCD. As stated before, exploring the arithmetic reasons why some 1-petal paths drift — and specifically only in x-axis — while others close is outside the scope of this particular article, but it remains an area for future research.
Closure when GCD(S, 6) > 1
This case is easier to analyze. Imagine the turtle completes one full block of S steps. It is now displaced by the sum of the vectors until this point, and is also facing to the slot that is the cumulative sum of orientation changes.
Since the squared-turn rule is periodic modulo S, the second block of S steps will trace a trajectory identical to the first — except it will be rotated by the constant offset angle θ(S). Now, picture a straight line drawn from the origin to the turtle's current displaced position. If we let the turtle run for another block of S steps, it will land at a new coordinate and face to the slot 2×θ(S).
Because the second S-step path is an identical (but rotated) clone of the first block, the imaginary line connecting the ending points of the first and the second S-step blocks will have the exact same length as the first imaginary line. Furthermore, since the turtle's starting heading was rotated by θ(S), this new connecting line will be rotated by that exact same angle when compared to the first imaginary line. If we continue this way, each S-step block pushes the turtle along the vertices of an imaginary regular polygon. Depending on GCD(S, 6), this rotational angle will be exactly 1/2, 2/3, or 1/6 of a full turn.
- GCD = 6 ⇒ θ(S) = S / 6 mod S This equates to 1/6 of a full rotation. The turtle will close its path after 6S steps, yielding a six-petal hexagonal rosette.
- GCD = 3 ⇒ θ(S) = 2S / 3 mod S This is a 2/3 rotation (or effectively a 1/3 rotation in reverse direction). The path closes after 3S steps, forming a three-petal equilateral triangle structure.
- GCD = 2 ⇒ θ(S) = S / 2 mod S This is exactly a 1/2 rotation. The turtle closes the loop after 2S steps, producing a two-petal linear symmetry.
You can experiment with the exact geometry of these three rotational closures using the tool below:
There is something interesting in the graphs we see above. There are shapes with 1, 2, 3, and 6 petals as expected by the analysis above, but we also see 4 and 12 petal shapes. We'll explain the underlying reason in the next two sections.
7 — Origin of Reflection Symmetries
The previous section accounts for the top-level, rotational symmetries of the paths our turtle draws. But when we look at a single petal, we can easily see that it has reflection symmetry too. This section will investigate the reasons of this common property.
Δθ(n) is palindromic
The orientation change function, Δθ(n), holds an interesting property: it is palindromic inside every S-step block. Reading the sequence of turns forward yields the exact same list as reading it backward. The proof requires only a single line of algebra:
This means the turn executed at step n is the same as the the turn executed at step S − n for 0 ≤ n ≤ S. In a S-step block, the first turn matches the last, the second matches the penultimate, and so on so forth.
To be precise, there is a slight caveat to the block's palindromic nature. When analyzing a complete S-step block, it only forms a palindrome if we omit the very first rotation step, where the turtle turns by 0² = 0 slots. To maintain the palindromy of the sequence, we must either drop this initial zero, or append an extra zero-slot turn to the tail of the sequence. The following table illustrates this for S = 6:
| n | n2 | n2 mod 6 |
|---|
The table demonstrates that discarding the initial 0 makes the sequence palindromic, but leaves us with S - 1 turns. Interestingly, this leading zero is the thing that forces the path to be reflectionally symmetric, as we'll see below.
Petal Reflection Symmetry
A geometric path consisting of S straight segments contains exactly S-1 internal joints (the corners between the lines). I'll show —in a rather informal way — that if the sequence of angles dictating these S-1 internal joints forms a palindrome, the resulting path will be reflection symmetric. The initial zero turn merely dictates the entry rotation of the entire shape.
Let's think about the initial S-step block, since each consecutive blocks will be identical but rotated and translated in space. It begins with zero orientation change and steps forward. Then, it follows S-1 step walk with palindromic sequence of turns.
Now, stop the turtle at its final position and force it to retrace its steps backward. To do this, the turtle must first flip its heading 180°, then step forward. At the first joint it encounters on the walk back, it must execute the inverse of the turn it made on the way forward. If Δθn represents the n-th internal turn, the palindromic property guarantees that Δθn = ΔθS - n. Comparing the forward and backward instruction sequences looks like this:
Backward: -ΔθS-1, -ΔθS-2, …, -Δθ1
Applying the palindrome identity to the backward sequence simplifies it:
Therefore, the turtle retracing its steps mathematically executes the exact same sequence of turn magnitudes, in the exact same order, just with negative signs. If the forward-moving turtle turns k slots clockwise at a joint, the backward-moving turtle turns k slots counter-clockwise at that corresponding joint. If you think about it, this is what we would see if we were to place a mirror passing through the middle point of the S-step path (which should have the correct angle, of course). Since the path is inherently identical to its own mirror image, the shape itself must possess reflectional symmetry.
If the skectchy proof above seems off, try the following interactive demo to have a better idea on this property. It demonstrates how our "turn then step forward" algorithm require a single anchor value prepended to a palindromic sequence to make reflectionally symmetric paths.
8 — Doubling of the Petal Count
Let's look at S = 24. We know from our previous "proof" that each petal (a block of 24 steps) inherently possesses reflectional symmetry. Yet, upon closer inspection, each petal has two smaller sub-petals for this specific S, each with its own internal reflection symmetries. In this section we'll analyze what causes this.
The Second Shift Identity
The palindrome property (n² ≡ (S−n)²) is responsible for the petal's outer mirror symmetry. But, for some S there is a second modular identity in the arithmetic, responsible for the case that petal consist of two reflectionally symmetric sub-petals:
The proof is a simple modular algebra: (n + S/2)² = n² + Sn + S²/4. The middle term, Sn, is a clean multiple of S, so it vanishes mod S. The final term, S²/4, vanishes mod S if and only if 4 is a divisor of S. ∎
Translated to geometry: whenever S is divisible by 4, the squared angular shift applied at step n is identical to the shift applied at step n + S/2. The second half of the petal has the exact same set of relative turns as the first half, just executed from a new origin point and rotation. Therefore, the second half of every petal is a rotated clone of the first half. This is why the path for S=24 (divisible by 4) visibly consist of 12 petals, while S=6 (not divisible by 4) remains with 6 petals.
Outer symmetry: True for all S. Derived from the palindrome identity n² ≡ (S−n)².
Sub-petal splitting: Requires 4 | S. Derived from the shift identity (n+S/2)² ≡ n².
This explains the petal counts observed in the examples considered here. Normally, we would have 1, 2, 3, or 6 petals. However, the shift identity doubles some 2 and 6 petals into 4 and 12. The 3-petal paths retain their count. Therefore, the observable set of visual petals is {1, 2, 3, 4, 6, 12} — the divisors of 12.
Seeing It Side-by-Side
9 — Related and Future Work
The results discussed here — closure conditions, petal counts, and reflection symmetries — can be analyzed within a broader mathematical framework: the theory of exponential sums.
If you treat each step's directional heading as a complex number positioned on the unit circle, the geometric position after n steps is nothing more than the partial sum:
Because θ(n) is a cubic polynomial, this is technically an exponential sum with a cubic polynomial. The academic study of these sums is vast, with roots going back to the works of Gauss and Weil. While specific families of cubic polynomials are analyzed in the literature, to the best of my knowledge, there is little published analysis focusing explicitly on why the geometric graphs of these partial sums consistently creates these symmetric rosette-like shapes.
The closest parallel exploration I have found is a piece called Exponential Sums by David Angell. He utilizes alternate cubic polynomials to generate similarly striking visual graphs. To keep this analysis accessible, I focused on a simplified polynomial defined by a quadratic change per step rule.
A fully expanded, interactive tool featuring variable turning functions, animation settings, and custom color modes is available at https://adnanbaysal.github.io/angle-path.html .
Future Directions
There are lots of open questions related to what we studied here. The following are the most obvious ones that came to my mind:
- Can the theoretical results in the exponential sums literature be used prove the geometric properties we discovered here?
- What geometric properties we would have if used a random cubic rational polynomial? What about higher degree rational polynomials?
- What is the precise arithmetic mechanism that causes certain 1-petal configurations to close while others to drift?
- Why drifting graphs only drift on the X-axis? What determines the drifting direction to be positive or negative?
- It is entirely possible to introduce an auxiliary integer R (where GCD(R,S) = 1) and process R S full turns instead of 1 S turns as base slots. The set of all possible turns will be the same, but the visuals generated will be significantly different. In fact, the interactive sandbox linked above natively supports both R and S inputs. Analyzing the structural impact of R remains another frontier for future research.