Teachers of statistics know that a bag of M&M candies is often the best resource available for discussing practically any topic in the curriculum. In honor of “no sweets” week here at school, I created a virtual bag of M&Ms. You can set the size of each bag (in number of candies less than 100) and you’ll get an assortment of blue, orange, green, yellow, red, and brown candies that follows the officially stated distribution for milk chocolate M&Ms!
Category Archives: Programming
NFL Scoring Combinations
Last Sunday, I watched the New York Giants play the Dallas Cowboys. The Giants began the game by scoring on a safety (2 points) and a field goal (3 points). And so in the first quarter, the scoreboard showed the Giants leading by the peculiar score 5-0. I noted there was only one way a team could score exactly five points (disregarding the order of the scores), and that brought up a math question I think I’ve been asking ever since I started watching football: how many ways are there to score n points in a game of American football?
There are five possible scoring “units” in the NFL: 2 (safety), 3 (field goal), 6 (touchdown and failed point after), 7 (touchdown and successful point after), and 8 (touchdown and successful two point conversion). I turned to Python to count some combinations. Here are the number of combinations for scores up to 15:
| Score | Ways | Score | Ways |
|---|---|---|---|
| 0 | 1 | 8 | 4 |
| 1 | 0 | 9 | 4 |
| 2 | 1 | 10 | 5 |
| 3 | 1 | 11 | 5 |
| 4 | 1 | 12 | 8 |
| 5 | 1 | 13 | 7 |
| 6 | 3 | 14 | 11 |
| 7 | 2 | 15 | 11 |
And since the most number of points scored in a game by a single team is 73, I computed the number of combinations for each score and made the following plot.

There are 1107 ways to score 73 points in the NFL, which seems out of line with my intuition (I think that’s too many). Maybe I need to double check my program or my intuition.
Tracing Orbits in the Mandelbrot Set
I recently added a new feature to my Mandelbrot Set script to trace the orbits of complex numbers through the plane. This along with the ability to zoom in, zoom out, or translate along the plane has made this a great teaching tool so far.
Some of my observations about the orbits:
- Points well outside the Mandelbrot Set are fairly mundane (as expected).
- Points along the edge of the main cardioid seem to offer the most visually appealing orbits (as shown above).
- Points close to but not inside the Mandelbrot Set show the most chaotic behavior.
I remember learning about the Mandelbrot Set in high school, but this is the first time I’m seeing this particular type of visualization. It’s very exciting!
The Antibuddhabrot
The Antibuddhabrot is similar to the Buddhabrot in that it is another representation of the Mandelbrot Set. The difference is that the Antibuddhabrot follows the trajectories of those complex numbers that are a part of the Mandelbrot set. In programming terms, this means changing a single less-than sign to a greater-than-or-equal-to sign.
Here’s a 100 million pixel exposure using a maximum iteration value of 128.
The corresponding Buddhabrot image I made was 2000 by 1600 pixels and took over two hours to render. This Antibuddhabrot is 750 by 600 pixels and took almost six hours to render!
Notes
The Buddhabrot and Antibuddhabrot images were created in Python while the basic Mandelbrot images were created with JavaScript. You can play around with the Mandelbrot Set at this page.
PNG and Compression
A single pixel on your computer screen represents three bytes of information. An image that’s 4096 pixels by 4096 pixels would take up 50.33 megabytes on your hard drive. Image formats typically find ways to compress this information so that file sizes don’t get out of hand.
I made some images in PNG format to try to get a sense of how well the compression works. As a starting point, I took an image that was entirely white and found that it took up 58,823 bytes. This is roughly one tenth of one percent of the space the “raw data” would take up, which seems pretty good.
Next I made two images, one that’s half white and half black and the other that’s made up of alternating white and black lines. These images have been scaled down from their original sizes, which is why it’s hard to discern the separate lines in the second image.
There were two things of note. First, the half white/half black image takes up only 53,999 bytes, which is actually less than the image that was a single color even though it may seem that the half/half image has “more” information. Second, the lined image takes up 61,890 bytes. So even though both images above have equal areas of white and black, the PNG format does not treat them equally.
I made two more images. Below, the one on the left is made up of white and black pixels spread randomly throughout the image. So again, each color takes up approximately the same area as the two images above. The image on the right contains all 16,777,216 colors possible on a computer monitor, one per pixel.
The image on the left is roughly 3.8 megabytes, approximately 60 times the size of its cousins. The image on the right takes up 36.4 megabytes! I wonder if this is the largest possible size of a 4096 by 4096 PNG image.
The Buddhabrot
The Buddhabrot is an alternate way to view the Mandelbrot Set. I won’t get into a full explanation of how these images were generated as I think Melinda Green’s page on the technique and the Wikipedia Buddhabrot page do a better job than I ever could. I made these images in Python using a modified version of the program I used to create images of the Mandelbrot Set.
This is a five million pixel exposure using a maximum iteration value of 20:
And here’s a 100 million pixel exposure using a maximum iteration value of 128:
On Melinda Green’s page, she writes:
I’m the most unreligious person you could ever meet, but it’s hard not to think of this image as revealing god hiding in the Mandelbrot Set and proving that the Hindus were right all along.
I’m not sure I agree with that sentiment (or even if Green is being facetious), but there definitely is something weird going on here.
This Buddhabrot gallery has a compilation of images in much higher resolution.
SET Distribution
These data come from 1,000,000 trials in Python. There’s a nonzero chance of eight, nine, ten, eleven, or twelve sets appearing among 12 random cards. The frequency of 12 sets though is less than 1 in 1,000,000, which makes the twelve set puzzle I posted before exceptionally rare.
What’s interesting to me: the distribution looks binomial.
The Mandelbrot Set
In the short time that I’ve been blogging, I’ve noticed an increase in my motivation to learn all the things I’ve always wanted to learn. Here, for example, is an image of the Mandelbrot Set I created using JavaScript. It’s something I’ve always wanted to do but I never bothered to learn enough about the Mandelbrot Set (or about JavaScript) to actually do it. Now here it is, and it really didn’t take that much time at all. Here’s the page that generated the image above.
Now that I have the basic code written, there are all sorts of things I can do with it. Least of which is just zooming in on some interesting features:
As an aside, here’s a rendering of the Mandelbrot Set done with a cookie and some candy!
Project Euler Problem #6
Project Euler keeps statistics on its users and the problems they’ve solved. Here’s one thing I found interesting. The five most common programming languages used are:
- C/C++
- Python (!)
- Java
- C#
- Haskell
I am also impressed with the 958 users who are programming in BASIC, the 653 who are using pencil and paper, the 459 who use Pascal, and the 5 users who grew up in a time when they still taught COBOL!
Here’s problem #6:
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Essentially, I’m evaluating:

Because 100 is such a small upper limit, this problem can be solved trivially by brute force. The difference turns out to be 25,164,150.
What’s perhaps more interesting is that there’s an identity for the sum of the squares of first m natural numbers. I found this in the problem notes, which you get access to once you’ve solved a certain problem. The identity is:

That’s new to me!
Skunk!
I did not come up with this game, but I can’t quite remember where I first heard about it either. This implementation is mine though. You can also click on the image to see the spinner.
In the game of Skunk!, players spin a wheel that is marked with $100, $500, and Skunk! spaces. Landing on a space with a dollar value adds the corresponding amount to a temporary pot. Repeatedly spinning accumulates more and more money in this pot. Before every spin, the player has a choice to make: take the money in the pot (and end the game) or spin again. However, if a player lands on Skunk!, the pot is emptied and he or she goes home with nothing!
Games like this are my favorite way to introduce a host of concepts in probability theory: expected value, variance, and the geometric probability distribution. The crucial question: at what point should you take the money and run?
Notes
You can see the HTML, CSS, and JavaScript that control everything by viewing the source on this page. In class, I have paper money to keep track of the pot. It’s actually quite fun to hit the big SPIN! button on a SmartBoard.















