GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes multiples project (three five) project euler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multiples project (three five) project euler" Watch "multiples project (three five) project euler" New topic
Author

multiples project (three five) project euler

Tim Hoang
Greenhorn

Joined: Feb 24, 2011
Posts: 26
for some reason i cant get my code to execute with the right answer. I get a continuous output of the same digit
my purpose:
1.get the variables
2.get highest possible multiple for each number. divide max possible with five and three
3.As the values are higher than 0 it should keep executing the if statement
4.The product of three add up to totalSum and the products of five add up to totalSum
4.the highest possible multiple of each (f, t) should decrease each by one.
5.repeat the if statement as it will always stay true til zero
6.By the time they are both zero, there are no more products to add and make the if statement false then executes else.
7.Else statement should then get the totalSum then output that value




Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
How do you know your code provides the wrong answer?


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Tim Hoang
Greenhorn

Joined: Feb 24, 2011
Posts: 26
"I get a continuous output of the same digit"

the code should stop after it hits the else and it is only a 3 digit number. I am suppose to get all the products of three and five and add them together
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Tim Hoang wrote:the code should stop after it hits the else


Then introduce a break statement like this:



Vishwa


Blog
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Is '=+' the same as the operator '+='? (No it's not.)

Did you mean '+='?
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331



whereas,


i.e in "=+" is not a single Java operator. You should read it as assignment of a positive number.

like how

int a=-5; would mean assigning a negtive number
Tim Hoang
Greenhorn

Joined: Feb 24, 2011
Posts: 26
I looked it over countless times but for some reason I have a little extra over 7000 thats not the answer.
240600 != 233168 (answer)
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Obviously, you're frustrated. We've all been there. Don't give up.

Since you're getting the wrong answer, there's a good chance that the solution you've coded is not correct. Please state the problem exactly as it was given to you. Also, let us know how you know the "right" answer. Sometimes the answer book is wrong.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

I assume you are doing Project Euler problem 1:

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.


There are a couple problems with your algorithm. you are setting f to 200 and t to 333. then you decrement both, so eventually, you will get to f at 0, and t at 133. your condition will fail, and so you will have missed many of the multiples of 3.

But additionally, you are also adding in some factors twice. 600, for example, is a multiple of BOTH 3 and 5, and your current method will add it in twice.

One thing I often do when working on these problems (and i've done the first 60 or so) is to test my algorithm against the smaller case. You know what the answer should be for using 10 instead of 1000. Change your code on lines 14 and 15 to use 10, and see if you get the right answer.

Another thing you can do is to print the values your adding in each time - so after line 22 print out totalFive and after line 24 print totalThree. By setting your original limit to 10, you can see if a) you are getting everything you should, and b) that you are not counting any values twice.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiples project (three five) project euler