programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Euler Problem One

Greenhorn
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 ]

Rancher
Posts: 2240
28
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
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
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.

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
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 ]

lowercase baba
Bartender
Posts: 12565
49
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:

Ryan James
Greenhorn
Posts: 15
Editted my posts accordingly, didn't notice the button. Apologies.

Ranch Hand
Posts: 457
& - bitwise and
| - bitwise or
&& - boolean and
|| - boolean or

(x % 3 == 0) - boolean value