programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Jeanne Boyarsky
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Bear Bibeault
• Tim Cooke
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Piet Souris
Bartenders:
• Frits Walraven
• Himai Minh

# multiples project (three five) project euler

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
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

Bartender
Posts: 563
• Number of slices to send:
Optional 'thank-you' note:

Tim Hoang
Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
"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

Ranch Hand
Posts: 331
• Number of slices to send:
Optional 'thank-you' note:

Tim Hoang wrote:the code should stop after it hits the else

Then introduce a break statement like this:

Vishwa

Greg Brannon
Bartender
Posts: 563
• 1
• Number of slices to send:
Optional 'thank-you' note:
Is '=+' the same as the operator '+='? (No it's not.)

Did you mean '+='?

Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
I looked it over countless times but for some reason I have a little extra over 7000 thats not the answer.

Greg Brannon
Bartender
Posts: 563
• Number of slices to send:
Optional 'thank-you' note:
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.

lowercase baba
Posts: 12986
66
• Number of slices to send:
Optional 'thank-you' note:
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.

 The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way: Thread Boost feature https://coderanch.com/t/674455/Thread-Boost-feature