# Project Euler Problem 1 - Haskell

#### Posted on January 10, 2010

##### Last updated on February 12, 2013

This is the first tutorial about solving the problems on Project Euler

I will be solving problem number 1, using the language Haskell, which is a functional programming language. If you come from a imperative programming language background (C++, C, Java, PHP), you might find Haskell a little ‘weird’ at first, but it is actually quite good for quickly solving some of the Project Euler problems!

Problem Description:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Solving this using Haskell is really simple. Open up ghci (Glasgow Haskell Compiler Interpreter), and then create somewhere a new file called euler1.hs, or whatever else you please.

Here is the code:

Line 1 says that the function euler1 is of type Int (think of it as just an integer), meaning it will return an integer after calling it. Line 2 is the meat of the function. Firstly, we are using the “list comprehension” syntax. Ignore “sum” for now, and just look at what’s inbetween the square brackets. Read it as “Return me a list of numbers x, where x comes from the finite list 1 to 999, and such that x is evenly divisible by 5 OR x is evenly divisible by 3.

Finally, use the predefined sum function. This function takes a list of numeric values, and returns the sum of them, which is precisely what we want.

As this is my very first tutorial like this, I would appreciate any comments, such as should I write more in-depth, less in-depth, explain some concepts more, explain how to set up Haskell on your system ?

Thanks, and please come back for the second tutorial for Project Euler!

Markdown SHA1: 7d9eca9c16d76e12eb26cc9367b09f2ffec7219e