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

Threaded View

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

    Default Circular prime checking algorithm optimization.

    I need help with my algorithm for finding circular primes. If you don't know what a circular prime is, it's when a number, say 179, and its rotations, 179, 917, and 791, are all prime. The algorithm simply checks if the number is a circular prime. The problem I'm using it for is finding the sum of all circular primes under 1,000,000. It's fast enough up until around 100,000, around 6 seconds. What can be done to speed it up?

    /**
    	 * @param num
    	 * @return true if the number is a circular prime
    	 * @return false if the number is not a circular prime
    	 */
    	public static boolean isCircularPrime(long num)
    	{
    		String number = Long.toString(num);//string containing the number
    		if(!isPrime(num))//false if the number isn't prime
    		{
    			return false;
    		}
    		else//checks other possibilities 
    		{
    			if(num>=10)//numbers with two or more digits can only be a circular prime if they are made of 1, 3, 7, or 9
    			{
    				if(number.contains("2")||number.contains("4")||number.contains("6")||number.contains("8")||number.contains("5")||number.contains("0"))//false if it contains a positive number, five, or zero
    				{
    					return false;
    				}
    				else//checks more if it doesn't contain an "illegal" number
    				{
    					for(int i = 0; i < number.length(); i++)//loops it for the length of the number
    					{
    						num = rotate(num);//rotates the number once
    						if(!isPrime(num))//false if the rotation isn't prime
    							return false;
    					}
    					return true;//true if it the number and all rotations are prime
    				}
    			}
    			else
    			{
    				return true;
    			}
     
    		}
     
    	}
    	//rotates the number once
    	public static int rotate(long number)
    	{
    		long start = number;
    		int numdigits = (int) Math.log10((double)number); // would return numdigits - 1
    		int multiplier = (int) Math.pow(10.0, (double)numdigits);
    		int num = 0;
    		long q = number / 10;
    		long r = number % 10;
    		//1234 = 123;
    		number = number / 10;
    		number = number + multiplier * r;
    		num = (int) number;
    		return num;
    	}

    Sieve of Eratosthenes to get a list of the primes.

    //shows all prime numbers to a specified max
    	public static int[] primesUpTo(int s) 
    	{
    		// initially assume all integers are prime
    		boolean[] isPrime = new boolean[s + 1];
    		for (int i = 2; i <= s; i++) 
    		{
    			isPrime[i] = true;
    		}
    		// mark non-primes <= N using Sieve of Eratosthenes
    		for (int i = 2; i*i <= s; i++)
    		{
    			// if i is prime, then mark multiples of i as nonprime
    			// suffices to consider mutiples i, i+1, ..., N/i
    			if (isPrime[i]) 
    			{
    				for (int j = i; i*j <= s; j++) 
    				{
    					isPrime[i*j] = false;
    				}
    			}
    		}
    		int primes = 0;
    		for (int i = 2; i <= s; i++) {
    			if (isPrime[i])
    			{
    				primes++;
    			}
    		}
    		int[] nums = new int[primes];
    		int index = 0;
    		for(int i = 2; i <= s; i++)
    		{
    			if (isPrime[i])
    			{
    				nums[index] = i;
    				index++;
    			}
     
    		}
     
    		return nums;
    	}

    I get the prime list fast enough, 32 ms. for 1,000,000.
    Last edited by aesguitar; July 25th, 2012 at 09:43 PM.


Similar Threads

  1. Recursive Fibonacci sequence optimization
    By aesguitar in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 24th, 2012, 08:49 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. Java Quick Sort Optimization
    By javaNewb37 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2011, 07:32 AM
  4. A Star Pathfinding algorithm optimization
    By randomcracker in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 18th, 2011, 11:11 PM
  5. Swing app gui optimization
    By nik_meback in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 12:55 PM