# 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.
sum = 0
threeMultiple = False
fiveMultiple = False
for i in range (3, 1000):
threeMultiple = i % 3 == 0
fiveMultiple = i % 5 == 0
if (threeMultiple or fiveMultiple):
sum += i
print (sum)
DOWNLOAD
Or you could do this in a one-liner:
print (sum ( [i for i in range(3, 1000) if i%3 == 0 or i%5 == 0] ) )
DOWNLOAD
The print function simply prints the return of the sum([]) function.
The sum([]) function takes in an array, sums up the values of each element in the list and returns that (the sum - a single numeric value).
The list to pass to sum([]) is generated in the following code:
[i for i in range(3, 1000) if i%3 == 0 or i%5 == 0]
This says that the list will include every i in the for loop from 3 to 999 of i
if i is a multiple of 3 and/or a multiple of 5. It loops through 3 to 999, checks if i is a multiple
of 3 and/or 5, and adds it to the list if it is.
After the list is created, the sum of that list is returned and printed.
Created: February 10, 2014
Completed in full by: Michael Yaworski