This puzzles come from the Project Euler website.
If we list all of the positive integers (whole 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.
If you would like some help the following are some hints that may be of use:
(creating a loop to run over all numbers)
We did this in the How many nines? puzzle. Have a look at the first hint there.
(testing if a number is a multiple of 3)
The mod operator % calculates the remainder on division. We can use this to check if the remainder is zero. For example
for k in range(1,10):
print(k, k % 3)
will print out the numbers 1, 2,3, ..., 10, and their remainder when is divided by 3.
(calculating the sum of the multiplies of 3)
This is similar to the code to compute the total in How many nines? we
for loops) we create an indentifier, say total=0, to store our result.for loops) we add the current number, k, to total whenever we find that k is a multiple of 3.for loops) we output the value of total.
(calculating the sum of the multiplies of 5)
To help you debug, the sum of the multiples of 5 up to but not including 1000 is 166833.
(calculating the sum of the multiplies of 3 and of 5)
One way to compute the total of the multiplies of 3 and of 5 is to separetly compute the total of the multiples of 3, and the total of the multiples of 5. Then add both totals. However this well mean that you have included some numbers twice - all of the multiples of both 3 and 5, i.e., multiples of 15. So if you also compute the total of the multiples of 15 you could subtract this to get your required answer.
However, it is easier to use the python operator or to test for both multiples of 3 and multiples of 5 in one if statement.