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.
In [16]:
result = 0
for i in range(0,1000):
if i % 3 == 0 or i % 5 == 0:
result = result + i
print("The sum of all the multiples of 3 or 5 below 1000 is " + str(result))
Above: I use a for loop to run through all numbers, i, from 0 to 999 by using range(0,1000). Then an if statement and modulo to check if i is divisible by 3 or 5. If it is divisible I add i to the result.
In [18]:
print(sum(i for i in range(1000) if i % 3 == 0 or i % 5 == 0))
In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.