So I have this problem here:
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.
And I wrote this here:
def multiples(num, below):
counter = 1
z = 0
while True:
x = num * counter
if x < below:
z += x
else:
break
counter += 1
return z
below = 1000
print "Multiples of 3: " + str(multiples(3, below))
print "Multiples of 5: " + str(multiples(5, below))
print "Added: " + str(multiples(3, below) + multiples(5, below))
If I set below to 10 I get the right answer, 23
Multiples of 3: 18
Multiples of 5: 5
Added: 23
But when I set it to 1000 I get this:
Multiples of 3: 166833
Multiples of 5: 99500
Added: 266333
And this is supposedly wrong, is there something I’m not getting?
Actually you need to remove multiples of 15 once below 1000, because it will be duplicated in both 3 & 5.. Which does not happens for below 10..
So, you can use a
Setto store those multiples, to remove the duplicate..