Multiples of 3 and 5

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.


Try other problems here .


The answer is 233168.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Chew-Seong Cheong
May 27, 2017

Using Python:

Zee Ell
May 27, 2017

Let's solve a more general problem by using the principle of inclusion-exclusion (PIE):

Find the sum S of the multiples of a and b below (N + 1) , and calculate the sum in our case (a = 3, b = 5, N = 999).

a , b , N N a, b, N \in \mathbb {N}

Let:

c = a × b c = a × b

A = N a = 999 3 = 333 , A = \lfloor \frac {N}{a} \rfloor = \lfloor \frac {999}{3} \rfloor = 333 ,

B = N b = 999 5 = 199 , B = \lfloor \frac {N}{b} \rfloor = \lfloor \frac {999}{5} \rfloor = 199 ,

C = N c = 999 3 × 5 = 66 C = \lfloor \frac {N}{c} \rfloor = \lfloor \frac {999}{3×5} \rfloor = 66

Then:

S = A ( A + 1 ) 2 × a + B ( B + 1 ) 2 × b C ( C + 1 ) 2 × c = S = \frac { A(A+1) }{2} × a + \frac { B(B+1) }{2} × b - \frac { C(C+1) }{2} × c =

S = 333 ( 333 + 1 ) 2 × 3 + 199 ( 199 + 1 ) 2 × 5 66 ( 66 + 1 ) 2 × 15 = 233168 S = \frac { 333(333+1) }{2} × 3 + \frac { 199(199+1) }{2} × 5 - \frac { 66(66+1) }{2} × 15 = \boxed { 233168 }

Remark: The problem can be further generalised to multiple factors by using PIE and the appropriate LCMs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>
int main()
{
    long unsigned int i,sum=0;
    clrscr();
    for(i=0;i<1000;i++)
    {
        if((i%5==0)||(i%3==0))
        {
            sum=sum+i;
        }
    }
    printf("%d\n",sum);
    getchar();
    return 0;
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class main
{
    public static void main(String[] args)
    {
        int total = 0;
        for(int i = 1; i < 1000; i ++)
        {
            if(i % 3 == 0 || i % 5 == 0)
                total += i;
        }
        System.out.println(total);
    }
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...