Collatz iterations

October 5th, 2009

Once again, I had a leisurely day at work. Decided to elaborate a simple alogirthm in Processing: the Collatz transformation. It’s really simple: if you can divide a number by 2, divide by 2. If not, multiply by 3 and add one. Repeat. Plot in a 1000 x 1000 grid in Processing. Result:

Plotted Collatz
(click for full-sized graph)

I calculated the numer of iterations for each number from 1 to 1 million using a console application in VB.Net. Below is the (simple) recursive algorithm that uses a class variable to keep track of the number of iterations.

Sub CalculateToOne(ByVal i As Long)
    aantalIteraties = aantalIteraties + 1

    If i Mod 2 = 0 Then
       i = i / 2
    Else
       i = i * 3
       i = i + 1
    End If

    If i = 1 Then Return
    CalculateToOne(i)
 End Sub

I saved numbers and iterations in a textfile, and created the above plot. Each point has a gray value depending on the number of iterations.

Even though I’m creating plots like this almost every week, and even though I know any patterns would have long been discovered, I’m still excited when I see visual patterns emerging. In this example there are patches that are darker, there’s the widening diagonal line from the top left, and horizontal streaks. If I were a briliant mathematician I’d probably flabbergast you with an explanation that’s harder to follow than the largest-but-one snowflake in a blizzard.

I’m not, though.

Uncategorized | Comments | Trackback

Leave a Reply

  1.  
  2.  
  3.  
You can keep track of new comments to this post with the comments feed.

About me

My name is Marco Hokke. My blog is about the things that interest me and things I might forget if I would not blog them.

Some of my favourite things are coding, photography, beer, theater and dance, Brian Eno's ambient music, Rammstein, purple, and reading.

Archives

Blogroll

Comics

Sites

Rijksmuseum

Meta