# Generating Prime Number

Leigh Castillo

Greenhorn

Posts: 2

posted 2 years ago

- 0

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.

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.

posted 2 years ago

- 0

Your tests (

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 (

(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.)

`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.)

It is sorta covered in the JavaRanch Style Guide. |