wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Euler Problem One Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Euler Problem One" Watch "Euler Problem One" New topic
Author

Euler Problem One

Ryan James
Greenhorn

Joined: Apr 18, 2007
Posts: 15
I'm getting back into Java and felt the Euler Problems may be the best way now sadly I'm 'stuck' on problem one.



Now this is printing

0
1
3
6
10
15
21
28
36


So obviously it's gone x = 1 so result = 1, x =2 so result is 3, x =3 so result = 6. Taking the previous 'x' adding 1 and adding that to the previous result.

Can anyone help point out where the code might be going wrong please - no code please.
[ August 07, 2008: Message edited by: Ryan James ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
where the code might be going wrong

Add some System.out.println() to show you the program's progress.
That should show you where the code might be going wrong.
Ryan James
Greenhorn

Joined: Apr 18, 2007
Posts: 15
I am assuming my code:

for (x = 0; x < 1000; x++)
{
if ( (x % 3 == 1) & (x % 5 == 1) );

Actually means

while x is less than 1000 add 1 to x, and if x is divisible by 3 and also 5 then add x to result. The problem is supposed to be simple, do I need something to stop x being added to the result if it doesn't match the %3 and 5?
Ryan James
Greenhorn

Joined: Apr 18, 2007
Posts: 15
For adding two println's I get:




0
0
0
1
0
1
2
1
3
3
3
6
4
6
10
5
10
15
6
15
21
7
21
28
8
28
36
9


Which was what I figured was happening, but I'm not sure why.

Now I've added:


Which seems to have problems at the moment but maybe this will help the issue, assuming that code will eventually go "so if x isn't divisible by 3 or 5 then the result stays the same,
[ August 07, 2008: Message edited by: Ryan James ]
Ryan James
Greenhorn

Joined: Apr 18, 2007
Posts: 15
This has now been solved:


}

}

So basically my problem was the & symbol and the fact I had a ; at the end of the for statement.
[ August 07, 2008: Message edited by: Ryan James ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

Ryan,

Please learn to use the code tags. clicking those little buttons below the box you type in your post will drop them in for you, then past your code between. it's the difference between this:

method()
{
for (int x = 0;x<10;x++)
{
System.out.println();
}
}

and this:


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ryan James
Greenhorn

Joined: Apr 18, 2007
Posts: 15
Editted my posts accordingly, didn't notice the button. Apologies.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
& - bitwise and
| - bitwise or
&& - boolean and
|| - boolean or

(x % 3 == 0) - boolean value


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Euler Problem One
 
Similar Threads
Project Euler problems with Scala
Project Euler #17
Project Euler Problem 25
Problem 382 of Euler project
Project Euler : Problem 23