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

Thread: Need a more efficient algorithm for prime numbers.

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need a more efficient algorithm for prime numbers.

    Here is my current algorithm, the TimeExec is just a class to print out how long it takes to run the code:

    public class Primes {
     
    	/**
    	 * Get a set of prime numbers.
    	 * @param no the number of primes to create
    	 * @return an array containing the requested number
    	 * of primes
    	 */
    	public static int[] getPrimes(int no) {
    		int[] primes = new int[no];
    		int primeInx = 0;
    		int i = 2;
     
    		if (primeInx < no) {
    			primes[primeInx++] = 1;
    		}
     
    		while (primeInx < no) {
    			boolean prime = true;
    			for (int j = 2; j < i; j++) {
     
    				if (i == i / j * j) {
    					prime = false;
    				}
    			}
    			if (prime) {
    				primes[primeInx++] = i;
    			}
    			i++;
    		}
     
    		return primes;
    	}
     
    	public static void main(String[] args) {
    		new TimeExec(new Runnable() {
    			public void run() {
    				int[] primes = getPrimes(1000);
    			}
    		}, "Get 1,000 primes", System.out).start();
     
    		//the 10,000 primes took the library's lab comp 19.125s
    		new TimeExec(new Runnable() {
    			public void run() {
    				int[] primes = getPrimes(10000);
    			}
    		}, "Get 10,000 primes", System.out).start();
     
    		new TimeExec(new Runnable() {
    			public void run() {
    				int[] primes = getPrimes(100000);
    			}
    		}, "Get 100,000 primes", System.out).start();
     
    //		new TimeExec(new Runnable() {
    //			public void run() {
    //				int[] primes = getPrimes(1000000);
    //			}
    //		}, "Get 1,000,000 primes", System.out).start();
    	}
    }

    I need to get it to be able to print out the 1,000,000 primes fairly quickly. Any help would be awesome, thank you!
    Last edited by helloworld922; September 29th, 2012 at 11:39 AM. Reason: please use code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need a more efficient algorithm for prime numbers.

    There must be algorithms that google could find. Try that.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need a more efficient algorithm for prime numbers.

    Try using a sieve. These are more memory intensive but they're very fast at finding prime numbers less than x.

    For something fairly simple see Wikipedia: Sieve of Eratosthenese.

    A little bit of self-promotion, but for moderately heavy prime generation (basically an optimized Sieve of Eratosthenes with a small hand-coded wheel factorization), see Optimizing the Sieve of Eratosthenes.

    And for your balls-out bonkers version, see Prime Sieve, which I think is the current record holder for consecutive prime number generation. From what I can tell it's a multi-threaded Sieve of Eratosthenes with advanced wheel factorization. Unfortunately it's implemented in C++, not Java.
    Last edited by helloworld922; September 29th, 2012 at 12:26 PM.

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Need a more efficient algorithm for prime numbers.

    Sieves work great for finding lists of primes up to n, much faster than the method you are currently using (for a list of primes to 1000000, sieving returns the list in 64ms on my machine, while brute force returns it in 643 ms). However, I can give you some pointers if you want with your currents algorithm:

    for (int j = 2; j < i; j++) {
     
    				if (i == i / j * j) {
    					prime = false;
    				}
    			}

    First, you upper bound is way to high. j need only go to the square root of i to check all possible factors. Also

    if (i == i / j * j) {
    					prime = false;
    				}

    can be changed to

    if (i % j == 0) {
    					prime = false;
    				}

    That percent sign thingy is called a modulo. What it does is it, in this case, divides i by j and returns the remainder. So, for example, 7 % 2 = 1 because when you long divide, you get 3 remainder 1 or simply 3 and 1/2. If i evenly divides into j, then it will always be zero, if not, then it will be some integer > 0.

    One other thing with building the list your way, using the ArrayList class will be much, much easier than using that array.

Similar Threads

  1. Circular prime checking algorithm optimization.
    By aesguitar in forum Algorithms & Recursion
    Replies: 5
    Last Post: July 26th, 2012, 11:35 AM
  2. Find prime nums from 1-100 algorithm
    By The-EyE in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 5th, 2012, 11:39 PM
  3. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  4. trying to generate prime numbers
    By yingyang69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 28th, 2011, 12:21 PM
  5. prime numbers
    By tdz013 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2011, 11:24 AM

Tags for this Thread