Zach White
Zach White

Reputation: 51

Sum of all the multiples of 3 or 5 below 1000

Beginner here- trying to make a simple python program that will compute/answer this problem:

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.

Currently this is what I have:

a = 0
b = 0
while a < 1000:
    a = a + 3
    print (a)

while b < 1000
    b = b + 5
    print (b)

This will print all the numbers being considered. I just need to add them together and that's my answer.

I would like one of two things to happen, instead of the code that I have written:

  1. I would like all of this to happen internally, and therefore not have to use the "print" function. The just print the sum of all of those multiples.
  2. I would like all of this stuff to print, but then I want to be able to print the sum of all of them too. Is there a way to make the computer take the value of everything it has printed?

Upvotes: 4

Views: 19523

Answers (7)

bero
bero

Reputation: 33

 **with python**

print(sum([i for i in range(1,1000) if i%3==0 or i%5==0 ]))

Upvotes: 0

shamiya manaf
shamiya manaf

Reputation: 11

total=0
i=0
while i<1000:
    if i%3==0 or i%5==0:
        print(num)
        total+=i
    i+=1
print("Total is: ")

Upvotes: 0

Lawal
Lawal

Reputation: 1

def sum_multiply (n):
    data = []
    for num in range (1, n):
        if num % 3 == 0 or num % 5 == 0:
            data.append(num)
    return sum(data)

sum_multiply(1000)

Upvotes: 0

Eugene Sh.
Eugene Sh.

Reputation: 18381

Actually this problem can be solved in O(1) instead of O(N) without using any loops or lists:

The required sum is the sum of all multiples of 3 plus sum of all multiples of 5 minus the sum of multiples of (3*5=15) below the given number 1000 (LIMIT=999). The sums are calculated as a sum of arithmetic series. It can be calculated in following way:

LIMIT=999

# Get the upper bounds for the arithmetic series
upper_for_three = LIMIT // 3
upper_for_five = LIMIT // 5
upper_for_fifteen = LIMIT // 15

# calculate sums
sum_three = 3*upper_for_three*(1 + upper_for_three) / 2
sum_five = 5*upper_for_five*(1 + upper_for_five) / 2
sum_fifteen = 15*upper_for_fifteen*(1 + upper_for_fifteen) / 2

# calculate total
total = sum_three + sum_five - sum_fifteen

# print result with Python 3
print(int(total))

The result is:

>>> 
233168

Upvotes: 12

Justin Niessner
Justin Niessner

Reputation: 245519

While a for loop would work, you could also use a generator expression and sum:

sum(n for n in range(1000) if n % 3 == 0 or n % 5 == 0)

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 994917

It is possible to do this in one line in Python using a generator expression:

print(sum(x for x in range(1000) if x % 3 == 0 or x % 5 == 0))

The range(1000) produces all the integers from 0 to 999 inclusive. For each one of those integers, if it is divisible by 3 or divisible by 5, then it is included in the result. The sum(...) function adds up all those numbers, and finally print(...) prints the result.

Upvotes: 4

Cory Kramer
Cory Kramer

Reputation: 118031

I would use a for loop to iterate over each number in your selected range. Then you can check if the modulus % is equal to 0, meaning it has no remainder when divided by those values, if so, add it to the total.

total = 0
for num in range(1000):
    if num % 3 == 0 or num % 5 == 0:
        print(num)
        total += num

>>> total
233168

Upvotes: 1

Related Questions