# Prime number generator program in Java

Printable View

• April 27th, 2009, 11:26 AM
big_c
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

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;     }```
• April 27th, 2009, 11:37 AM
Freaky Chris
Re: confused about a couple things
Code :

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

```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
• April 27th, 2009, 11:38 AM
big_c
Re: confused about a couple things
omg i fell like such an idiot.
• April 27th, 2009, 11:42 AM
Freaky Chris
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
• April 27th, 2009, 11:43 AM
big_c
Re: confused about a couple things
any suggestions for the next prime method
• April 27th, 2009, 12:08 PM
Freaky Chris
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
Code :

`Math.round(Math.sqrt(n)/(Math.ln(Math.sqrt(n))-1)+0.5)`

Chris