We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Need help with Project Euler question.
Page Index Toggle Pages: 1
Need help with Project Euler question. (Read 3025 times)
Need help with Project Euler question.
Jun 17th, 2010, 7:58pm
 
Quote:
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.


I'm learning how to program by doing the math questions form Project Eular. I have no idea why this code doesn't calculate the correct answer:
Code:

void setup(){
 noLoop();
}

void draw(){
 int sum = 0;
 for (int x=0; x < 1000; x+=3){
   sum+=x;
   println ("x: " + x + " sum: " + sum);
 }
 //166833

 for (int x=0; x < 1000; x+=5){
   sum+=x;
   println ("x: " + x + " sum: " + sum);
 }
 //99500
}

//Total 266333



Please give me a tiny little hint!

Re: Need help with Project Euler question.
Reply #1 - Jun 17th, 2010, 9:27pm
 
think about 15
Re: Need help with Project Euler question.
Reply #2 - Jun 18th, 2010, 3:16am
 
Thanks! I feel dumb.
Re: Need help with Project Euler question.
Reply #3 - Jun 18th, 2010, 4:23am
 
no problem - happens to everyone  Smiley
Re: Need help with Project Euler question.
Reply #4 - Jun 18th, 2010, 6:51am
 
Two things:
1) why are you solving math problems and learning the basics of programming in processing I mean, sure there's nothing wrong with it, it just seems a bit odd if you ask me :p

2) You should really do some reading on the Modulo operator (%) http://en.wikipedia.org/wiki/Modulo_operation
http://processing.org/reference/modulo.html.
Your entire program can be written in 9 lines of code while at the same time giving the right answer Smiley

on a side note though: "multiples of 3 or 5" is that a normal "or" or a "exclusive or" (xor)
Page Index Toggle Pages: 1