Sum of all multiples of 3 or 5 below 1000

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

If we list all 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 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 read. I just need to add them together and answer this question.

I would like for one of two things to happen instead of the code I wrote:

  • I would like all this to happen internally and therefore no need to use the "print" function. Just print the sum of all these multiples.
  • I would like all of these materials to be printed, but then I want them to be able to print the sum of all of them. Is there a way to make the computer appreciate the value of everything it printed?
+4


source to share


8 answers


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

The required amount is the sum of all multiples of 3 plus the sum of all multiples of 5 minus the sum of multiples (3 * 5 = 15) below the specified number 1000 (LIMIT = 999). Sums are calculated as the sum of arithmetic series. It can be calculated as follows:

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))

      



Result:

>>> 
233168

      

+7


source


This can be done 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))

      



range(1000)

prints all integers between 0 and 999, inclusive. For each of these integers, if it is divisible by 3 or divisible by 5, it is included in the result. The function sum(...)

concatenates all these numbers and finally print(...)

outputs the result.

+4


source


I would use a loop for

to iterate over every number in the selected one range

. Then you can check if the modulus is %

0, that is, it has no remainder when divided by these 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

      

+1


source


While the loop for

is running, you can also use a generator expression and sum

:

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

      

0


source


Here's another way that applies to any number:

def multiples(number):
    sum = 0
    for x in range(number):
        if x % 3 ==0 or x % 5 == 0:
            sum += x
    return sum

      

0


source


import java.util.*;

class Main{
    public static void main(String[] args){
        int start=1;
        int end=1000;

        int sum=0;
        for(int i=start;i<=end;i++){
            if(i%3==0 || i%5==0)
            sum+=i;
        }
        System.out.print(sum);
    }
}

      

0


source


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)

      

0


source


Swift 3:

func sumMultiples() -> Int {
    var result = 0
    for i in 1..<1000 {
        if i % 3 == 0 || i % 5 == 0 {
            result += i
        }
    }
    return result
}

print(sumMultiples())   // will print "233168"


// PS:
// you can calculate the result on paper, the idea is:
// how many numbers 3 is multiplier for? 999/3 = 333
// how many numbers 5 is multiplier for? 999/5 = 199
// sum of 333 numbers is ∑(from 1 to 333)*3 = ( (a1 + aN)*N/2 )*3 = ( (1 + 333)*333/2 )*3 = 166833
// sum of 199 numbers is ∑(from 1 to 199)*5 = ( (a1 + aN)*N/2 )*5 = ( (1 + 199)*199/2 )*5 = 99500
// but we count numbers for which and 3 and 5 are multipliers, so we need to decrement result
// decrement sum of 66 (999/15 = 66) numbers ∑(from 1 to 66)*15 = ( (a1 + aN)*N/2 )*15 = ( (1 + 66)*66/2 )*15 = 33165
// result = 166833 + 99500 - 33165 = 233168

      

-2


source







All Articles