Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  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


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