Project Euler problem 9 in one line of Haskell

Problem 9 of Project Euler asks you to find the product of the sole Pythagorean triplet, the sum of whose values is 1000, where a Pythagorean triplet (familiar from elementary school math) is a set of natural numbers  a < b < c where a^2 + b^2 = c^2.

This snippet of Haskell solves it in one line and demonstrates some of what makes Haskell an interesting language, but takes several minutes to run on my MBP:

problem9 = take 1 [a * b * c | a <- [1..1000], b <- [1..1000], c <- [1..1000], a + b + c == 1000, a^2 + b^2 == c^2]

A version with some simple improvements runs much faster. Since a < b < c, we know the largest possible value for a is 332 (since 333 + 334 + 335 is 1002). And since b must be larger than a, we can narrow down its possible values as well, based on a. C does not need to be independently calculated at all, since by definition it must be a value that will satisfy the Pythagorean theorem. So:

problem9' = floor $ head [a * b * sqrt (a^2 + b^2) | a <- [1..332], b <- [(a+1)..(999-a)], a + b + sqrt (a^2 + b^2) == 1000]

This version runs in a couple seconds.

Hot buttered rye breakfast cocktail

After much experimentation, I have come close to replicating the deliciousness that is the Hot Buttered Rye breakfast cocktail from one of my favourite brunch joints in Brooklyn, Rye.

  • 1 tbsp butter
  • generous dash of ground cloves
  • 1.5 tsp of molasses
  • earl grey tea packet
  • rye (any whiskey) to taste

Melt the butter in your mug, add in the cloves and mix em up good. Stir in the molasses. Take a big whiff–it should smell good. If it doesn’t, you screwed something up.

Make the tea in this same mug, and let it sit for a little while. There should be white foamy stuff on top. If not, you screwed something up, but it’ll probably still taste decent.

Add in an ounce or two of the whiskey.

Imbibe and rejoice.