## Find the sum of all the multiples of 3 or 5 below 1000.

For questions about problems on the Project Euler web site. No spoilers. Please include the question number in the subject line of your post.

### Find the sum of all the multiples of 3 or 5 below 1000.

removed for spoilers
Last edited by mrlog101 on Thu May 30, 2013 2:01 pm, edited 1 time in total.
mrlog101

Posts: 3
Joined: Wed May 29, 2013 4:12 pm

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

Hello

Your result is right (if 1000 is included in the sum) and I have seen no mistake in your code
you also have the result with :
Code: Select all
`sum(set(range(0,1001,3)+range(0,1001,5)))`

Regards
MichelFJM

Posts: 19
Joined: Wed May 22, 2013 1:41 pm

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

Well the website I got the task from says it's incorrect, so I've either misunderstood the question or something else... I can't see what though.

The question is at http://projecteuler.net/problem=1 if you want to see and try to put the answer in.
mrlog101

Posts: 3
Joined: Wed May 29, 2013 4:12 pm

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

1000 is not below 1000.
Last edited by stranac on Thu May 30, 2013 2:00 pm, edited 2 times in total.
Reason: Moved topic to Project Euler
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

Oh right... well that was stupid of me.
mrlog101

Posts: 3
Joined: Wed May 29, 2013 4:12 pm

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

Quicker approach:

Code: Select all
`3 * 333 * 167 + 5 * 100 * 201 - 15 * 33 * 67`
codingismycraft

Posts: 6
Joined: Fri May 23, 2014 3:25 pm

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

codingismycraft wrote:Quicker approach:

Code: Select all
`3 * 333 * 167 + 5 * 100 * 201 - 15 * 33 * 67`

Agreed but in my book you still fail the exercise, because you are not explaining why you get there so the poor future programmer that lacks you genius and has to maintain your code may not understand how you got here. There are usually two outcomes: you code gets enshrined to the point that no-one dares rewriting the application to any extent(*), or someone rewrites your code badly, and introduces numerous obscure bugs that plague the applications for years.

(*) I know an application somewhere that runs on a farm of Linux servers, but the core is on a Windows computer (likely XP) because it was written using the IBM C++ Compiler for Windows and OS/2, using its proprietary string library (STL was unheard of when the application was initially written).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Ofnuts

Posts: 2358
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

Ofnuts wrote:
codingismycraft wrote:Quicker approach:

Code: Select all
`3 * 333 * 167 + 5 * 100 * 201 - 15 * 33 * 67`

Agreed but in my book you still fail the exercise, because you are not explaining why you get there so the poor future programmer that lacks you genius and has to maintain your code may not understand how you got here. There are usually two outcomes: you code gets enshrined to the point that no-one dares rewriting the application to any extent(*), or someone rewrites your code badly, and introduces numerous obscure bugs that plague the applications for years.

(*) I know an application somewhere that runs on a farm of Linux servers, but the core is on a Windows computer (likely XP) because it was written using the IBM C++ Compiler for Windows and OS/2, using its proprietary string library (STL was unheard of when the application was initially written).

OK.. Quick documentation of this approach:

Sum of multiples of 3 that are less than 1000:

SUM_3 = 3 * ( 1 + 2 + ... + 333)

Sum of multiples of 5 that are less than 1000:

SUM_5 = 5 * ( 1 + 2 + ... + 200)

Using the formula:

1 + 2 + ... + n = n * ( n-1) / 2

We have:

SUM_3 = 3 * 333 * 334 / 2 = 3 * 333 * 167

SUM_5 = 5 * 200 * 201 / 2 = 5 * 100 * 201

C = SUM_3 + SUM_5

Multiples of both 3 and 5 are encountered twice in C, so we need
to subtract it from it. We can find their total by calculating
the SUM_15 (exactly as we did for SUM_3 and SUM_5):

SUM_15 = 15 * ( 1 + 2 + ... + 66)

or

SUM_15 = 15 * 66 * 67 / 2 = 15 * 33 * 67

So:

Sum = SUM_3 + SUM_5 - SUM_15
codingismycraft

Posts: 6
Joined: Fri May 23, 2014 3:25 pm

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

codingismycraft wrote:[
Sum of multiples of 5 that are less than 1000:

SUM_5 = 5 * ( 1 + 2 + ... + 200)

That includes 1000, when it shouldn't, doesn't it?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Ofnuts

Posts: 2358
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

### Re: Find the sum of all the multiples of 3 or 5 below 1000.

Ofnuts wrote:
codingismycraft wrote:[
Sum of multiples of 5 that are less than 1000:

SUM_5 = 5 * ( 1 + 2 + ... + 200)

That includes 1000, when it shouldn't, doesn't it?

Yes, it is inclusive as the other solution that was presented before.

Obviously to make it non inclusive we take the sum for

SUM_5 = 5 * ( 1 + 2 + ... + 199)

and

SUM_15 = 15 * ( 1 + 2 + ... + 65)
codingismycraft

Posts: 6
Joined: Fri May 23, 2014 3:25 pm