Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Euler Problem One

 
Ryan James
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1345
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Editted my posts accordingly, didn't notice the button. Apologies.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
& - bitwise and
| - bitwise or
&& - boolean and
|| - boolean or

(x % 3 == 0) - boolean value
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic