Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    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.

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

    Did you enjoy this article?

    Continue reading about Collatz, Haskell and Memoization

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

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

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

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

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

    Created by Swizec with ❤️