Monthly Archives: April 2010

Twitterable Mandelbrot II: The Mandelbrottening

Yesterday I posted my Twitterable Mandelbrot, a ruby script that generates the Mandelbrot Set in 134 characters. A few of you took this as a challenge to shorten my code even further. I didn’t mind, and in fact was interested to see your results; I was sure that an extra character here or there could be shaved off.

What I didn’t expect at all was that somebody would shave fourteen characters off.

Reader brahbur on rubyflow came up with this:

a couple of these changes could be considered “cheating” (-:

80.times{|a|p (0..300).map{|b|x=y=i=0;(x,y,i=x*x-y*y+b/150.0-1.5,2*x*y+a/40.0-1,i+1)until(x*x+y*y>4||i>98);i>98?0:1}*''}

Brahbur’s solution does look different; there are quote marks on each line and it outputs 1s and 0s instead of #s and .s, but the mandelbrot is still clearly visible (Edit: I reduced the size from 300×80 to 240×60 just to keep the outputs roughly the same size):

Click for larger version (1400×800)

I think this is just awesome. Once we’re playing with 0’s and 1’s, I can see another optimization: i>98?0:1 can be replaced with 99i. This bring us down to 118:

60.times{|a|p (0..240).map{|b|x=y=i=0;(x,y,i=x*x-y*y+b/120.0-1.5,2*x*y+a/30.0-1,i+1)until(x*x+y*y>4||i>98);99i}*''}

I have to give most of the credit to brahbur, though–I just saw a tiny tweak, on top of the amazing rewrite they already did. So great. THANK YOU brahbur!

Now, the challenge continues: can you shorten this further? Brahbur was concerned about “cheating”, so let’s define the rules for clarity: Output should be 240×60 (extra quotes and padding are okay) and it should be visually recognizable as a Mandelbrot set. Other than that, go for it.

Twitterable Mandelbrot

As a kid I always thought fractals were neat, but every time I tried to learn how to do them, I got lost in the math. I guess 20 years makes all the difference: today I went and read up on the Mandelbrot set and had one of those “wait, that’s it?” moments.

It took me about 15 minutes to write the program. Here’s the output:

Click for larger version (1400×800)

The whole program was about 400 characters long. I got to thinking, “that’s *almost* small enough to fit into a single tweet…” and then I spent the next hour and a half refactoring my code for size.


What do you mean I need a hobby? I have one. See?

Terrible Beauty

Recently I dumped all of the data out of one of our geographic tables in our database, and plotted a red dot for every record. This was the result:

Click the link for a larger version (1200×800).

This is an interesting graph. It appears to follow population, but there are also some patterns that follow state outlines: there are some curious voids in North Carolina and Minnesota, for example. In the west, we can almost make out highways; there’s a visible north/south track through Denver along I-25, and there’s a strong clustering down I-15 through Utah with a big blotch over Las Vegas.

Kinda neat, huh?

What you are looking at is every registered sex offender in the United States. My job is to collect and analyze that data, along with crime information.

Still neat? Sigh.

There are still some interesting patterns, however. Population centers follow the freeways in the western states. As the population density rises to the east, we lose track of individual roads; sex offenders are only absent where people cannot live. Physical barriers like coastlines and bays are clearly marked, of course; but you can also clearly see places where people merely aren’t allowed to live: the Okefenokee National Wildlife Refuge in Florida, the Allegheny and Susquehannock National Forests in Pennsylvania, and the cluster of tiny national parks all throughout upstate New York.

I am most interested, however, in North Carolina and Minnesota. Why are they so obviously underrepresented here? Are people just less likely to offend sexually in those states? Possibly; I am too cynical to believe that people in those states behave differently than in the other 48, but it could be that NC and MN convict sex offenders less often, or they could simply have less strict sex offender laws.

Ah, but see how trusting you are of my little dots? It could be that the NC and MN sex offender registry systems are just harder to get data from. While I haven’t worked with those states directly, I have worked on the code that aggregates this data, and I can tell you that no two states provide data the same way. Some states are sensible and reliable, while others are such a headbashing nightmare—Alaska, I am looking at you here—that you wonder if their IT department had to make do with wild monkeys because the trained ones were too expensive.

Here’s a really good possibility: Some states track sex offenders at different levels, and sex offenders under a certain level are tracked inside the state but not published to national registries. It could be that those two states have just as many RSO’s per capita, but they do not report misdemeanor offenses outside the state.

Or, who knows. It really could be that in North Carolina people are too nice—and in Minnesota it’s just too cold—for that sort of thing.

Note: some of the dots lie outside the US. This is not always a mistake. Sex offenders are required to keep their address registered even if they move outside the USA. There are a few thousand US sex offenders scattered across the globe—including one at the South Pole.