File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes NEED HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "NEED HELP" Watch "NEED HELP" New topic


kory frost

Joined: Sep 27, 2006
Posts: 1
i am creating a java code for school and i need it to disply only prime numbers from 1 to 100 so far i have this

can anoyne solve my code?

[ EJFH: Added "[ CODE][ /CODE]" tags and fixed formatting. ]
[ September 27, 2006: Message edited by: Ernest Friedman-Hill ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> can anoyne solve my code?

please specify the problem you have
Jason Cox
Ranch Hand

Joined: Jan 21, 2004
Posts: 287
Kinda sounds like a homework assignment.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

It's OK if this is homework; just don't expect anyone to do it for you. If you're interested in help, then that's cool.

I added our UBB "CODE" tags to your post and fixed up the formatting to make your code a bit easier to follow.

The truth is you have a pretty good start here, and the major problem to be dealt with is that the printing and testing in the innermost nested loop shouldn't be where it is. Look at the code and think hard about when you want to do what -- in other words, after I've done all of X, then I should do Y. To make something happen after a loop runs all the way through, you'd need to put the code for that something outside the closing bracket at the end of the loop. Some things can happen outside of one loop, but inside of another, if you get my meaning.

[Jess in Action][AskingGoodQuestions]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
I wrote this program a while ago, but I'm not going to give you the solution since I will be banned from the site. Anyway, this is a tad complex and requires a little thought.

One improvement you should make, you only need to test for divisor upto the square root of the number you're testing.
so if X is the number to test, the biggest number to check for division will be (int)Math.sqrt(X)

That will make a huge difference to efficiency if you're testing for primes of size 1,000,000 or more.
I agree. Here's the link:
subject: NEED HELP
It's not a secret anymore!