## Creating Fractals II: Recursion vs. Iteration

There was such a positive response to last week’s post, I thought I’d write more about creating fractal images.  In the spirit of this blog, what follows is a mathematical “stream of consciousness” — that is, my thoughts as they occurred to me and I pursued them.  Or at least a close approximation — thoughts tend to jump very nonlinearly, and I do want the reader to be able to follow along….

Let’s begin at the beginning, with one of my first experiments.  Here, the counterclockwise turns are 80 degrees, and the clockwise turns are 140 degrees.

One observation I had made in watching PostScript generate such images was that there was “overlap”:  the recursive algorithm kept going even if the image was completely generated.  Now the number of segments drawn by the recursive algorithm is a power of 4, since each segment is replaced by 4 others in the recursive process.  So if the number of segments needed to complete a figure is not a power of 4, the image generation has to be stopped in the middle of a recursive call.

This reminded me of something I had investigated years ago — the Tower of Hanoi problem.  This is a well-known example of a problem which can be solved recursively, but there is also an iterative solution.  So I was confident there had to be an iterative way to generate these fractal images as well.

I needed to know — at any step along the iteration — whether to turn counterclockwise or clockwise.  If I could figure this out, the rest would be easy.  So I wrote a snippet of code which implemented the recursive routine, and output a 0 if there was a counterclockwise turn, and a 1 if there was a clockwise turn.  For 2 levels of recursion, this sequence is

0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0.

The ones occur in positions 2, 6, 8, 10, and 14.

I actually looked at 1024 steps in the iteration, and noticed that the ones occur in exactly those positions whose largest power of 2 is odd.  Each of 2, 6, 10, and 14 has one power of 2, and 8 has three.

You might be wondering, “How did you notice that?”  Well, the iterative solution of the Tower of Hanoi does involve looking at the powers of 2 within numbers, so past experience suggested looking along those lines.  This is a nice example of how learning neat math can enlarge your mathematical “toolbox.”  You never know when something might come in handy….

There was other interesting behavior as well — but it’s simpler if you just watch the video to see what’s happening.

First, you probably noticed that each of the 18 star arms takes 32 steps to create.  And that some of the star arms — eight of them — were traversed twice.  This means that 18 + 8 = 26 arms were drawn before the figure was complete, for a total of 832 steps.  Note that the recursive algorithm would need 1024 steps to make sure that all 832 steps were traversed — but that means an overlap of 192 steps.

Now let’s see why some of the arms were traversed twice.  The 32nd step of the first arm is produced after 31 turns, and so the 32nd turn dictates what happens here.  Now the highest power of 2 in 32 is 5, which is odd – so a clockwise turn of 140 degrees is made.  You can see by looking at the first 33 steps that this is exactly what happens.  The 32nd step takes you back to the center, and then a 140 degree clockwise turn is made.

Now after the next arm is drawn, the turn is determined by the 64th angle.  But 6 is the highest power of two here, and so an 80 degree counterclockwise turn is made — but this takes you over the same arm again!

Note that we can’t keep traversing the same arm over and over.  If we add 32 to a number whose highest power of 2 is 6:

$2^6m+2^5=2^5(2m+1),$

we get a number whose highest power of 2 is 5 again (since 2m + 1 must be odd).  Since this power is odd, a clockwise turn will be made.

So when do we repeat an arm?  This will happen when we have a counterclockwise turn of 80 degrees, which will happen when the highest power of 2 is even (since an odd power takes you clockwise) — when looking at every 32nd turn, that is.  So, we need to look at turns

32, 64, 96, 128, 160, 192, 224, etc.

But observe that this is just

32 x (1, 2, 3, 4, 5, 6, 7, etc.).

Since 32 is an odd power of two, the even powers of two must occur when there is an odd power of 2 in 1, 2, 3, 4, 5, 6, 7, etc.  In other words, in positions 2, 6, 8, 10, 14, etc.

To summarize this behavior, we can state the following simple rule:  arms move seven points counterclockwise around the circle, except in the case of the 2nd, 6th, 8th, 10th, 14th, etc., arms, which repeat before moving seven points around.  Might be worth taking a minute to watch the video again….

We can use this rule to recreate the order in which the star arms are traversed.  Start with 1.  The next arm is 1 + 7 = 8.  But 8 is the 2nd arm, so it is repeated — and so 8 is also the third arm.  The fourth arm is 8 + 7 = 15, and the fifth is seven positions past 15, which is 4.  Mathematically, we say 15 + 7 = 4 modulo 18, meaning we add 15 and 7, and then take the remainder upon dividing by 18.  This is know as modular arithmetic, and is one of the first things you learn when studying a branch of mathematics called number theory.

The sixth arm is 4 + 7 = 11, which is repeated as the seventh arm.  You can go on from here….

There are still some questions which remain.  Why 32 steps to complete an arm?  Why skip every seventh arm?  Why are the arms 20 degrees apart?  These questions remain to be investigated more thoroughly.  But I can’t stress what we’re doing strongly enough — using the computer to make observations which can be stated mathematically very precisely, and then looking at a well-defined algorithm to (hopefully!) prove that our observations are accurate.  More and more — with the advent of technology — mathematics is becoming an experimental science.

I’ll leave you with one more video, which shows PostScript creating a fractal image.  But laying a mathematical foundation was important — so next week, we can look at how you can make your own fractals in Python using an iterative procedure.  This way, you can explore this fascinating world all on your own….

There are 10 levels of recursion here, and so 1,048,576 segments to draw.  To see the final image, visit my Twitter feed for October 9.  Enjoy!

## Creating Fractals

Recently, I’ve been working with a psychology student interested in how our brains perceive fractal images in nature (trees, clouds, landscapes, etc.). I dug up some old PostScript programs which reproduced images from The Algorithmic Beauty of Plants, which describes L-systems and how they are used to model images of plants. (Don’t worry if you don’t have the book or aren’t familiar with L-systems — I’ll tell you everything you need to know.)

To make matters concrete, I changed a few parameters in my program to produce part of a Koch snowflake.

The classical way of creating a Koch snowflake is to begin with the four-segment path at the top, and then replace each of the four segments with a smaller copy of this path. Now replace each of the segments with an even smaller copy, and recurse until the copies are so small, no new detail is added.

Algorithmically, we might represent this as

F  +60  F  -120  F  +60  F,

where “F” represents moving forward, and the numbers represent how much we turn left or right (with the usual convention that positive angles move counter-clockwise). If you start off moving to the right from the red dot, you should be able to follow these instructions and see how the initial iteration is produced.

The recursion comes in as follows: now replace each occurrence of F with a copy of these instructions, yielding

F  +60  F  -120  F  +60  F  +60

F  +60  F  -120  F  +60  F  -120

F  +60  F  -120  F  +60  F  +60

F  +60  F  -120  F  +60  F

If you look carefully, you’ll see four copies of the initial algorithm separated by turning instructions. If F now represents moving forward by 1/3 of the original segment length, when you execute these instructions, you’ll get the second image from the top. Try it! Recursing again gives the third image, and one more level of recursion results in the last image.

Thomas thought this pretty interesting, and proceed to ask what would happen if we changed the angles. This wasn’t hard to do, naturally, since the program was already written. He suggested a steeper climb of about 80 degrees, so I changed the angles to +80 and -140.

Surprise! You’ll easily recognize the first two iterations above, but after five iterations, the image closes up on itself and creates an elegant star-shaped pattern.

I was so intrigued by stumbling upon this symmetry, I decided to explore further over the upcoming weekend. My next experiment was to try +80 and -150.

The results weren’t as symmetrical, but after six levels of recursion, an interesting figure with bilateral symmetry emerged. You can see how close the end point is to the starting point — curious. The figure is oriented so that the starting points (red dots) line up, and the first step is directly to the right.

Another question Thomas posed was what would happen if the lengths of the segments weren’t all the same. This was a natural next step, and so I created an image using angles of +72 and -152 (staying relatively close to what I’d tried before), and using 1 and 0.618 for side lengths, since the pentagonal motifs suggested the golden ratio. Seven iterations produced the following remarkable image.

I did rotate this for aesthetic reasons (-24.7 degrees, to be precise). There is just so much to look at — and all produced by changing a few parameters in a straightforward recursive routine.

My purpose in writing about these “fractal” images this week is to illustrate the creative process in doing mathematicsThis just happened a few days ago (as I am writing this), and so the process is quite fresh in my mind — a question by a student, some explorations, further experimentation, small steps taken one at a time until something truly wonderful emerges.  The purist will note that the star-shaped images are not truly fractals, but since they’re created with an algortihm designed to produce a fractal (the Koch snowflake), I’m taking a liberty here….

This is just a beginning! Why do some parameters result in symmetry? How can you tell? When there is bilateral symmetry, what is the “tilt” angle? Where did the -24.7 come from? Each new image raises new questions — and not always easy to answer.

Two weeks ago, this algorithm was collecting digital dust in a subdirectory on my hard drive. A simple question resurrected it — and resulted in a living, breathing mathematical exploration into an intensely intriguing fractal world. This is how mathematics happens.