File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating Prime Number

 
Leigh Castillo
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I Just want to ask what is wrong about
my codes in generating the prime nos.


any reply would be appreciated. thanks!
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leigh, welcome to the Ranch!

Why don't you start by telling us why you think there is something wrong with your code. Does it not compile? Throw an exception when you run it? Read our FAQ entry TellTheDetails to find out about things which would be useful to anybody wanting to help out with the learning process.
 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your tests (i%j==0 and i==j) are true for every i, since your inner loop runs till j is equal to i.

It's not a good test either. Prime number is a number which is divisible only by one and itself. You should be looking for numbers (n) that do not have any divisor between 2 and n-1. Try to rewrite your condition that way.

(However, this is still quite inefficient way to find primes. There are many possible optimizations. For example, no prime except 2 is even, so you might put in 2 in advance and then only test odd numbers starting at three. And there are others. If you're really interested, I'd suggest doing a small research on prime numbers first.)
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of errors. It's good if you can study more about prime numbers..
 
Leigh Castillo
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys to the replies..

I already find out the code
I just need to put break when the no. didn't passed 2nd condition..

Thanks again ^^
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic