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.

Postby mrlog101 » Thu May 30, 2013 12:02 pm

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.

Postby MichelFJM » Thu May 30, 2013 1:10 pm

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.

Postby mrlog101 » Thu May 30, 2013 1:20 pm

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.

Postby stranac » Thu May 30, 2013 1:55 pm

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.
User avatar
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.

Postby mrlog101 » Thu May 30, 2013 1:57 pm

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.

Postby codingismycraft » Mon Jun 06, 2016 6:12 pm

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.

Postby Ofnuts » Mon Jun 06, 2016 8:26 pm

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).
See you soon on http://python-forum.io/
User avatar
Ofnuts
 
Posts: 2646
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.

Postby codingismycraft » Tue Jun 07, 2016 3:22 am

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.

Postby Ofnuts » Tue Jun 07, 2016 8:26 am

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?
See you soon on http://python-forum.io/
User avatar
Ofnuts
 
Posts: 2646
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.

Postby codingismycraft » Tue Jun 07, 2016 3:53 pm

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


Return to Project Euler

Who is online

Users browsing this forum: No registered users and 1 guest