# Project Euler, Problem #1

The first problem within Project Euler isn’t meant to twist the brain in knots and is a gentle introduction to what is coming further down the road. The problem states:

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.

As I mentioned in my initial post about exploring Project Euler, I noted that I often write in a lowest common denominator with whatever language I’m using at the time. The net effect of that decision is often a working but poorly optimised solution or a solution that doesn’t fit well with the spirit of the programming language.

The solution to problem #1 took three distinct evolutions, the first of which is below and looks like a solution you’d likely see in any programming language:

```i = 1
sum = 0

while i < 1000:
if i % 3 == 0 or i % 5 == 0:
sum += i
i += 1

print sum```

I wasn’t a fan of needing to declare the variables to start with for such a simple problem, so removed the need for the `i` but using the Python `range` function to generate a series of numbers. The `range` function allows you to generate any length series of numbers, starting and ending where you want with any stepping. By default, it starts at 0 and a stepping of 1 – which means `range(1000)` is going to generate a list of numbers from 0 – 999.

```sum = 0

for x in range(1000):
if x % 3 == 0 or x % 5 == 0:
sum += x

print sum```

Python provides functionality for its lists called list comprehensions. The idea behind a list comprehension is that you can perform the same action, whatever that may be, against each item in the set. In the final solution below, I have applied a filter (the `if` statements), which now only returns those items from the `for x in range` that match and I’m not applying an expression to the resulting matches (the solitary `x` before the `for`). Once collected the new list is passed to the `sum` function to yield our result.

`sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0])`

# Project Euler, An Exercise In Exploration

Some time ago I stumbled onto a math based problem solving site for programmers named Project Euler. The Project Euler site describes itself as:

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.

Possible solutions to the problems are verified through the Project Euler web site, where your successes are recorded if you sign up for an account. You don’t need to work through the problems in order, however doing so may be beneficial as work completed and previous questions is often reused.

I thought Project Euler would be a great exercise to explore more of the Python programming language. While I’ve read quite a bit about the Python language, I’ve not written anything substantial in it and I often find myself using the lowest common denominator in my approaches to solving problems as a by product.

My intention while working through the problems presented within Project Euler is to find a neater, cleaner method of solving the problem which is hopefully more pythonic. I’ll present my solution to the problems and with a little luck, I’ll receive some helpful improvements from the great programming community as well.