1

The problem sounds like this: "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."

I have written some code below and I'm confused as to why it doesn't work

s1 = 0
s2 = 0
for i in range(0,1000,3):
    s1 = s1 + i
for i in range(0,1000,5):
    s2 = s2 + i
suma = s1 + s2
print(suma)

On top of this, I tried assigning the value of 0 to all the variables at once:

suma, s1, s2 = 0
#the rest of the code here

And I'm getting the following error: TypeError: cannot unpack non-iterable int object

How do I assign everything at once? Do I even need to assign every variable to 0 in order for my code to work or is it similar to Turbo Pascal where every int variable automatically gets assigned 0?

Rektyyy
  • 41
  • 3
  • 2
    `s1, s2 = 0, 0` – theX Jul 19 '20 at 21:53
  • 3
    You will count 15, 30, 45, etc. twice the way you are doing it. – Mark Jul 19 '20 at 21:54
  • I'm not sure what people downvote this. You showed your work, formatted the question well, and had a reproducible problem. +1. – Mark Jul 19 '20 at 22:01
  • @theX you can also do multiple assignment in one line as follows: `suma = s1 = s2 = 0` – Aaron Jul 19 '20 at 22:02
  • Maybe it was downvoted because it is unclear, not useful, or shows no research effort. – mkrieger1 Jul 19 '20 at 22:07
  • 1
    I think you've forgotten what it was like to be a beginner @mkrieger1. The existing code might represent a fair amount of work. It's pretty unfair to say it shows no research effort. – Mark Jul 19 '20 at 22:10

2 Answers2

3

Counting each range one at a time counts the numbers that are multiples of both 5 and 3, like 15 and 30, twice.

A more python way to do this would be to imagine taking the range as a sequence and filtering out all numbers that don't meet your criteria. Then sum up the sequence. For multiples of 5 or 3 (or both), that criteria might look like:

n % 3 == 0 or n % 5 == 0

% produces the remainder divided by the following number. For any n this is only true if it is a multiple of 5 or 3. You can use that to filter the range and sum:

sum(n for n in range(10) if n % 3 == 0 or n % 5 == 0)
# 23

This works because the inner bit makes an iterator of just the values you want:

[n for n in range(10) if n % 3 == 0 or n % 5 == 0]
# [0, 3, 5, 6, 9]

And summing that with the builtin function sum() gives the correct answer.

Also the error you are getting is because this format of assignment tries to unpack several values:

a, b, c = [1, 2, 3] # works
a, b, c = 10 # error -- not enough values.
Mark
  • 90,562
  • 7
  • 108
  • 148
2

As Pythonic as @MarkMeyer's answer may be, let's fix your original code to produce the correct result. (And assign the value 0 to all the variables at once.)

s1 = s2 = s3 = 0

for i in range(0, 1000, 3):
    s1 += i

for i in range(0, 1000, 5):
    s2 += i

for i in range(0, 1000, 3 * 5):
    s3 += i

suma = (s1 + s2) - s3

print(suma)
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Thank you for grabbing my initial code and fixing it! I was a tad bit confused by Mark's code, but this clears it up. Also thank you for showing me the += operator, this will really clean everything up and I won't have to use ancient methods such as s1=s1+i anymore. – Rektyyy Jul 20 '20 at 06:41