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 3 of 3

Thread: Wondering if there's a way to make this more efficient.

  1. #1
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Wondering if there's a way to make this more efficient.

    I have the following method for determining whether or not an integer value is a prime number:

    public boolean isPrime(int value){
        if(value < 2)
            return false;
        else 
        {
            int xSquareRoot = (int)Math.sqrt((double)value);
     
            for (int x = 3; x <= xSquareRoot; x+=2)
                if(value % x == 0)
                    return false;
        }
        return true;
    }

    The for loop seems like it would be a waste of time and resources, and that it would be inefficient.

    I'm just wondering if there's maybe a way I could make this more efficient. I mean, it works very quickly now, but it just seems like iterating over every odd number up to the integer square root of a number seems like a resource drain, especially for large primes, where the code would have to do several thousand iterations.


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Wondering if there's a way to make this more efficient.

    Take a look at this link. Your algorithm falls under the "Naive methods" and can be improved.

    Do they not have google where you're at? This took about 30 seconds to find.
    Need Java help? Check out the HotJoe Java Help forums!

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Wondering if there's a way to make this more efficient.

    Remember if your function argument is int or smaller and your function return value is int or smaller, that there's *by definition* a static alternative on reasonably common hardware. Your "boolean isPrime(int)" for example could be implemented as a 128MB array. I'm not suggesting you add a 128MB object to your project, just responding to your question as-asked.

  4. The Following User Says Thank You to Sean4u For This Useful Post:

    cristian_ny95 (June 29th, 2012)

Similar Threads

  1. can anyone think of a more efficient way of doing this...
    By mia_tech in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 19th, 2012, 10:32 AM
  2. How to make code more efficient?
    By Apocalypse in forum Java Theory & Questions
    Replies: 2
    Last Post: October 21st, 2011, 09:07 AM
  3. How to make a simple and efficient web control panel.
    By kelsmith11 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 18th, 2011, 02:28 PM
  4. Replies: 4
    Last Post: September 5th, 2010, 10:29 AM
  5. begginer wondering why his guessing game won't work
    By Ligawulf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 8th, 2010, 12:22 AM