Project Euler #1


Tonight I decided I’d give the problems at Project Euler a go using Python…here is my first shot at problem 1:

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.

Code:

def sum_multiples(num1, num2, upper):
“””
pre: num1, num2, upper +ve integers
num1, num2 < upper returns: sum of all multiples of num1 and num2 below upper """ result = 0 for x1 in xrange(0, upper, num1): result += x1 for x2 in xrange(0, upper, num2): if x2 % num1 != 0: result += x2 return result[/sourcecode] Looking through some of the solutions on the forum, there were some really insightful and elegant ways proposed to retrieve the result, often reducing the problem to a single calculation. I'm really looking forward to seeing what comes out of the later solutions.

4 thoughts on “Project Euler #1

Leave a reply to geoffness Cancel reply