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.

Results 1 to 6 of 6

Thread: Prime number generator program in Java

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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;
     
     
    }
    Last edited by Deep_4; November 7th, 2012 at 01:45 PM. Reason: Please add code tags! Read my sig...


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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
    Last edited by Freaky Chris; April 27th, 2009 at 11:41 AM.

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: confused about a couple things

    omg i fell like such an idiot.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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. #5
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: confused about a couple things

    any suggestions for the next prime method

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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
    Last edited by Freaky Chris; April 27th, 2009 at 12:58 PM.

Similar Threads

  1. Replies: 3
    Last Post: February 26th, 2009, 03:04 AM
  2. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM