Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Prime number generator program in Java

1. ## Prime number generator program in Java

good afternoon everyone i am working on a prime number generator and i am confused on a couple things. first thing is i am getting an error for my Boolean isPrime statement. also i have to create a method to generate the next prime number here is what i have so far. i commented out the next prime until i get the first half to work. i figured no sense in trying to do two separate methods at once. heres my code

```/**
This class prints out all the prime numbers of an input value.
*/
public class PrimeGenerator
{
public PrimeGenerator()
{

}
{
boolean isPrime(int num)

boolean prime=true;

for(int x=2; x<num; x++)
if(num %x==0)
prime=false;

}
/**
Calculate the next prime number of an input.
@return the next prime number
*/

/*public int nextPrime()
{
do

while
}*/

private int num;

}```

2. ## Re: confused about a couple things

```{
boolean isPrime(int num)```
Should be
```boolean isPrime(int num)
{```

Also another note on Math. You only need to check upto the sqr root of the number, lets say for then number 81 you do not need to check all the numbers from 0-81 only 0 to root 81 which is 9

Thus making it much much much faster,

Regards,
Chris

3. ## Re: confused about a couple things

omg i fell like such an idiot.

4. ## Re: confused about a couple things

No worries, everyone makes synatically mis-takes from time to time especially when learning. Quite often it just needs a fresh pair of eyes to look at it.

p.s read my edit to previous post

Chris

5. ## Re: confused about a couple things

any suggestions for the next prime method

6. ## Re: confused about a couple things

Basically if you had to fnid the next prime after lets say 7. You would test each number after 7 until you found a prime using your isPrime() function.

P.S I Hope to post some maths on evaluating if a number is prime soon, so keep an eye out for my next post

Nvm, runs out the best maths I can give you is that the highest number you need to check is
`Math.round(Math.sqrt(n)/(Math.ln(Math.sqrt(n))-1)+0.5)`

Chris