Project Euler: Problem #1 – Multiples of 3 and 5

project euler

English: Python powered logo with print("...

The last Saturday I went to a Python workshop organized by the Toulibre in Toulouse. The experience was so great that I decide to start the Project Euler to learn this amazing language.

I will to try to publish each problem on Mondays. To make the challenge a little more interesting, I am going to use scientific libraries in Python like Numpy and Scipy.

Do you know other scientific libraries for Python? 

Please, I would like to know about these in the comments section!

Here is the solution for the Problem #1:

Problem 1 – Multiples of 3 and 5

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.

[sourcecode language=”python”]
import numpy

n = 1000
Num_mult_five = range (0, n, 5)
Num_mult_three = range (0, n, 3)
# We need to subtract once the multiples of 15
Num_mult_fifteen = range (0, n, 15)

Sum_mult_five_three = numpy.sum(Num_mult_five + Num_mult_three) – numpy.sum(Num_mult_fifteen)

print(Sum_mult_five_three)
[/sourcecode]

Go back to the list of problems.

Comments

Leave a Reply