Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NEED HELP

 
kory frost
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> can anoyne solve my code?

please specify the problem you have
 
Jason Cox
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kinda sounds like a homework assignment.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic