# multiples project (three five) project euler

Tim Hoang

Greenhorn

Posts: 26

posted 4 years ago

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

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

Posts: 563

Tim Hoang

Greenhorn

Posts: 26

Vishwanath Krishnamurthi

Ranch Hand

Posts: 331

Greg Brannon

Bartender

Posts: 563

Vishwanath Krishnamurthi

Ranch Hand

Posts: 331

Tim Hoang

Greenhorn

Posts: 26

Greg Brannon

Bartender

Posts: 563

posted 4 years ago

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.

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.

Always learning Java, currently using Eclipse on Fedora.

Linux user#: 501795

posted 4 years ago

I assume you are doing Project Euler problem 1:

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.

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