Skip to content
Swizec Teller - a geek with a

Collatz, Haskell and Memoization

xkcd collatz conjecture

After an awesome longboarding session yesterday afternoon I decided to play around with infinite sequences in Haskell - it's supposed to be one of the more (most?) powerful features of Haskell - because it's a lazy language apparently.

My first impulse of creating a primes generator was nipped in the bud by a long page of prime number generators in Haskell. Scary, complex, mindboggling.

Project Euler posed a much better challenge: Which starting number, under one million, produces the longest collatz chain?

The solution I came up with was a simple brute force generator of infinitely many collatz sequences. Then I would take the first 1,000,000 find the maximum and that's that.

collatz :: Integer -> [Integer]
collatz 1 = []
collatz n
| odd n = 3*n+1:collatz(3*n+1)
| even n = div n 2:collatz(div n 2)
chains = [collatz x | x <- [1..]]

Didn't help.

So I started looking for the maximum a bit differently - take all the sequences, sort them by length and take the last one.

longest max =
last $ sortBy (comparing snd) $ zip [1..] $ map length $ take max chains

Great! It worked! But it takes ~26 seconds!

Well sorting maybe isn't the best idea ever, so let's try creating a list of sequences where the list's tail only contains those sequences that are longer than the head. A sprinkle of dropWhile and it was done.

longest' (max_i, max_l) =
let l = head $ dropWhile (\(i,l) -> l <= max_l) $ zip [max_i..] $ map length $ chains' max_i
in l:longest' l

~25 seconds!

That's odd ... even odder still is running both algorithms one after another only takes 33 seconds. Huh?

It would seem I'm using memoization incorrectly. I've heard it performs funny in recursive functions. The theory I formulated last night was that because haskell was lazy each execution chain was constructed to its end and the intermittent memoized values never got used until the whole function was called again.

Looking at the code samples this morning, though, I discovered this:

collatz :: Integer -> [Integer]
collatz = memoize col where
col 1 = []
col n
| odd n = 3*n+1:collatz(3*n+1)
| even n = div n 2:collatz(div n 2)

As you can see, I don't call the memoized function internally. Just goes to show what a night's sleep can do to one's coding abilities. I bashed my head against this problem for four hours yesterday and I never noticed I was recursing to the wrong function!

Interestingly enough, fixing that makes the algorithm spaz out and die after 16 seconds. The only output I get is "Killed". Curious.

Enhanced by Zemanta

Did you enjoy this article?

Published on January 9th, 2012 in collatzconjecture, Haskell, Languages, Math, Memoization, Number Theory, Programming, Project Euler, Uncategorized

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet:

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️