Chaos Game v2

I prepared a small demonstration of the Chaos game for some math teachers. First we used transparencies, markers, dice, and rulers, but humans are mistake-prone and slow.

To The Computer!

I decided to experiment with the rules of the game to see where it’d go.

Human Error

What happens when you bring in a random amount of error based on the distance traveled? Can you predict the result? The further the distance, the more error in the calculation.

Random Square

Random Hexagon

Weighted Die

What happens when you use a weighted die? Can you tell which vertex is being chosen more often than the others?

Weighted Die Triangle

Weighted Die Hexagon

Generalized Chaos Game

Live Demo.

Screen Shot 2014-03-18 at 8.46.47 PM

By this time, I was getting frustrated at using different processing files just because I wanted to change the number of vertices. I had originally “cheated” to find the coordinates of each point by using a website to calculate the rectangular coordinates of a hexagon. I knew there must be an easier way, but last summer I was more focused on the creation of the images. I figured it out this time.

Here’s the code:

//create points 
  float h1 = random(0.95);
  for (float theta = 0; theta < TWO_PI; theta += TWO_PI / numOfPoints)
    //graph the points using polar form (rotate theta), then convert back to rectangular
    int xposition = int(radius * cos(theta))+width/2; //Add half width to move to 1st quad
    int yposition = int(radius * sin(theta))+width/2;

    h1 = h1 + 0.618033988749895; //Golden ratio!!
    h1 = h1 % 1;

    color c = color(h1,0.8,0.95);
    sp.add(new Spoint(xposition,yposition,c));

For those who are code-adverse, instead of trying to calculate the rectangular coordinates of a regular n-gon directly, I calculate the polar coordinates and then convert back to rectangular. Duh! Notice the theta going from 0 to TWO_PI, and going up by (TWO_PI / numOfPoints), and then going back to rectangular by using x = r*cos (theta) and y = r*sin(theta).

This code snippet also highlights a neat trick to make the random colors that were spread out nicely (idea from here). The problem is that using random to get a decimal from 0 to 1 doesn’t tend to spread the color values out, there are lots of repeat colors. But if you chose the first color randomly, and then rotate by the golden ratio (add the conjugate:  0.618033988749895), then you get colors that start off random, but are nicely spread out. Here’s an example of the problem. The top graph is twenty random numbers, and the bottom graph is one random number spread out to 20 points with help from the golden ratio.

Screen Shot 2014-03-18 at 8.42.22 PM


Some Interesting Results

Go to the generalized chaos game, and reduce the number of vertices to 4, and increase the distance by two clicks (60% of the way to the next random choice). You should get something like this:


Do the white lines occur at a geometric sequence? Why??

Now go to the hexagon, and increase the distance two clicks again, you should get something like this:


What shapes are the overlapping colors (and the primary colors)? Why?

Lastly, some fun patterns happen when you continue to decrease the distance. Eventually, each point will actually move away from the randomly chosen point. Here are two such versions.


Happy Accidents

While trying to make the generalized chaos program, I ran into the following by accident:

Unique Quad v1:


Unique Quad v2


Now It’s Your Turn

Find me something cool. Let me know about it. Enjoy!

Buffon’s Needles simulation in Processing

Buffon’s Needle is a famous way to (slowly) estimate \pi.
Here’s a program to calculate \tau (to keep the math-hipster hatred of \pi-day at a critical point and concave up).

Link to live simulation and code. All variables are easy to change, size of window, length of needle, spacing of lines, etc.


Neat program to write. Funnily enough, the code wasn’t working in the javascript version of processing because javascript didn’t understand the constant TAU. Ha.

note: I got the graph idea at the bottom idea from this sketch, but I never looked at the actual code.

Power Series on Desmos

This is a crosspost from my Photo 180 blog.

Power Series work in AP Calculus BC.


Process: Since it’s a infinite series, look at partial sums to get an idea what this graph looks like.
So look at

Perfect time to use technology.

Texas Instruments Method

Go to y1. Enter in y=x.
Wait 3-5 seconds.
Go to y1. Subtract a \frac{x^3}{3!}.
Wait 3-5 seconds.
Go to y1. Add on a \frac{x^5}{5!}.
Wait 3-5 seconds.
Go to y1. Subtract a \frac{x^7}{7!}.

2014-03-05_13h53_31 2014-03-05_13h53_44

REALLY crappy resolution. Awful zoom system. Where’s the factorial sign? Hopefully you remember what the previous graphs looked like. Lots of waiting. Ugh.

Desmos: version 1

Graph y=x
Subtract a \frac{x^3}{3!}.
Add on a \frac{x^5}{5!}.
Subtract a \frac{x^7}{7!}.

Desmos: version 2

Students teaching teachers: Have one your students find this out for himself, and remark that they can enter in the entire series, but he’s having trouble finding the infinity sign. SLIDERS!
Whoa. (That seemed like it should be much harder to type in. Took me 5 minutes to type in the original latex code at the beginning of this post. Took me < 1 minute to actually enter the series into desmos. I had to help 1 student out of 15. That’s it. Wow easy.)

Watch the power series create sin(x) step by step by moving a slider. LIVE!

We live in good times.


Two James Tanton Questions

It’s midterm week at school, and James Tanton threw out two interesting questions in two days. I spent a little time programming “solutions” to these problems (not solutions, just verifications for an infinitesimally small portion of the natural numbers).

Problem One:

Here’s my code for this problem. And here’s the output of the code, each time the sequence gets longer, it prints out the new “max” sequence length. 2014-01-28_08h29_27

I didn’t use any of’s graphics but I had the prime function optimized, so it was quick work.

Problem Two:

Here’s the python code for the “solution”. And here’s the last six lines of the output of the code. 2014-01-28_08h31_32 I checked all numbers under 1,000,000, and all the sequences were finite (they stopped at a multiple of 13). The starting number whose sequence ended in the largest multiple of 13 was 964,665, and the multiple of 13 had 384 digits (BIG NUMBER! There are only ~10^80 particles in the entire universe). Fun stuff.

[Edit: 1/28/2014 9:08am] Ok, ok. James asked for a proof for the second problem. Here you go :-) 2014-01-28 09-05-38

Prime Matrix – Processing

Saw this tweet yesterday:

Pretty cool. Josh Giesbrecht also did some great work with Mathematica to replicate the image.

I also replicated the image with The (live) code can be found here.


(update 1-16-2014 11:38, image above is now lossless and will stand up to zooming.)

Four and Five – Cartalk Puzzler

From cartalk.

Ray: Get a piece of paper and write the number four, leave a little space, and write the number five. What common mathematical symbol, when placed between the numbers four and five, will result in a number that is greater than four but less than six?

Tom: It has to be a mathematical symbol? It can’t be, like, the word “or”?

Ray: No, it’s got to be something that’s commonly used in mathematics. You’ve used it many, many times – maybe even today.

Pebbling a Checkerboard Game (Or Chessboard)

I happened upon this tweet when I got to work this morning:

So I watched the fantastic numberphile video Pebbling a Chessboard. I wanted to get the kids to play the game and I didn’t have any checkers in the classroom, so I decided to program the game in Processing. Thankfully the programming went smoothly, and I finished it in time to have the Pre-Calculus class try to beat the game (it’s the day before winter break, and we just finished sequences and series). Fantastic timing.


  • Goal: remove all checkers from the green prison.
  • If you click on a checker, and there is space to the right and above, then that checker will disappear, and two clones will appear to the right and above.
  • Theoretically this game board extends to infinity to the right and above.

The game is hosted by or hosted by It should work in any web browser, including smartphones. If you’d like to increase the checkerboard size, just go to the site and “tweak” the code. The size of the board is set on the first two lines.


Why I Blog (Nowak requested post)

Fellow Empire State rep Kate Nowak requested answers to the following in her post: Tell Me Why You Blog.

1. What hooked you on reading the blogs? Was it a particular post or person? Was it an initiative by the nice MTBoS folks? A colleague in your building got you into it? Desperation?

I got hooked on reading math blogs because I was interested in becoming a better teacher and I wasn’t finding resources in other places to satisfy this desire. Math blogs put me in touch with teachers willing to try new things out and succeed, or more importantly, fail in public.

2. What keeps you coming back? What’s the biggest thing you get out of reading and/or commenting?

Confession: I don’t comment much on blogs. Maybe I use twitter as a sounding board for my opinions? Not sure why.

I keep coming back because I keep getting good stuff. The economics of this system are tough, if you people in the #mtbos stop producing good stuff, then I’m out. Thankfully, the stuff coming out has only been getting better and more interesting.

3. If you write, why do you write? What’s the biggest thing you get out of it?

Mostly I write for myself. I process things better after writing about them. This is from a guy who hates to write (or reflect). I record things on my blog first for myself; second for others. If other people find use in my blog, fantastic. But I’d still have some version of it if it were only my Mom reading it (hi Mom!).
It’s great that people find use for things I’ve posted. Just this morning I woke up to find 15+ responses on twitter talking about a post that I put up 3 years ago. Soooo cool. Just awesome. And that there’s that stupid Oreo thing. That must count for something.

4. If you chose to enter a room where I was going to talk about blogging for an hour (or however long you could stand it), what would you hope to be hearing from me? MTBoS cheerleading and/or tourism? How-to’s? Stories?

Dunno. Give me personal stories. I’m no help here.