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 [4]:
lst = range(1000) # This creates a list that ranges from 0 to 999.
new_lst = [] # This defines a new, empty list
for n in lst: # This for statement loops through every number in the original list, lst
if n % 3 == 0 or n % 5 == 0: # Then, for every number in lst, this if statement says that if the number is divisible by either 3 or 5, then it will be added to the new list, new_lst
new_lst.append(n)
sum_multiples = 0 # This defines a new variable, sum_multiples, and sets it to zero
for i in new_lst: # This for statement loops through every number in new_lst and adds it to sum_multiples
sum_multiples += i
print(sum_multiples) # Finally, we print sum_multiples, which is the sum of all the multiples of 3 or 5 below 1000
In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.