Previous Page

The Sieve of Atkins - Java console

Copy and Paste the following code into your favorite Java Integrated Development Environment (IDE) - compile and run.

/***********************************************************************************
/ Name: Brian Butler
/ Class: 9:00am - 10:45 
/ Professor: Gary Hartell
/ Date Finished: May 2, 2011
/
/ Assignment:Sieve of Atkin
/
/					   					 Descripion:
/		This program uses the Sieve of Atkin to find all of the prime numbers up to
/		 and including the integer entered by the user.
/
/
************************************************************************************/

/***********************************************************************************
/							Algorithm:(taken from Wikipedia.com)
/
/		1. Create a result list filled with 2, 3, and 5.
/		2. Create a sieve list with an entry for each positive integer;
/			all entries of this list should initially be marked nonprime.
/		3. For each entry number n in the sieve list, whith modulo-sixty remainder r:
/			a. If r is 1,13, 17, 29, 37, 41, 49, or 53, flip the entry for each
/				possible solution to 4x^2 + y^2 = n
/			b. If r is 7, 19, 31, or 43, flip the entry for each possible solution
/				to 3x^2 + y^2 = n
/			c. If r is 11, 23, 47, or 59, flip the entry for each possible solution
/				to 3x^2 - y^2 = n when x > y
/			d. If r is something else, ignore it completely
/		4. Start with the lowest number in the sieve list.
/		5. Take the next number in the sieve list still marked prime.
/		6. Include the number in the results list.
/		7. Square the number and mark all multiples of that square as nonprime.
/		8. Repeat steps five through eight.
/
************************************************************************************/

import java.util.*;//arrays copy
import javax.swing.JOptionPane;
public class atkinsSieve 
{
    public static void main(String[]args)
    {
        int[] primes;
        String arraySizeStr;
 	   int arraySize;		
		     arraySizeStr=
			JOptionPane.showInputDialog("Enter a number to find all the prime" + 
	  							 " numbers up to that number.");
		   arraySize = Integer.parseInt(arraySizeStr);                 
                primes = AtkinSieve.findPrimes(arraySize);
					 for(int prime : primes)
					 {
					 System.out.print(prime + " ");
					 }
					 System.out.println();
					 System.out.print("DONE!");
	}
      static public class AtkinSieve
	 {     
        public static int[] findPrimes(int arraySize)
	   {
            boolean[] isPrime = new boolean[arraySize + 1];
            double sqrt = Math.sqrt(arraySize);
		  
            for (int x = 1; x <= sqrt; x++)
		  {
                for (int y = 1; y <= sqrt; y++) 
			 {
                    int n = 4 * x * x + y * y;
                    if (n <= arraySize && (n % 12 == 1 || n % 12 == 5)) 
			     {
                        isPrime[n] = !isPrime[n];
                    }

                    n = 3 * x * x + y * y;
                    if (n <= arraySize && (n % 12 == 7))
			     {
                        isPrime[n] = !isPrime[n];
                    }

                    n = 3 * x * x - y * y;
                    if (x > y && (n <= arraySize) && (n % 12 == 11))
			     {
                        isPrime[n] = !isPrime[n];
                    }
                }
            }
            for (int n = 5; n <= sqrt; n++) 
		  {
                if (isPrime[n])
			 {
                    int s = n * n;
                    for (int k = s; k <= arraySize; k += s) 
			     {
                        isPrime[k] = false;
                    }
                }
            }
            int[] primes = new int[arraySize];
            int found = 0;
            if (arraySize > 2)
		  {
                primes[found++] = 2;
            }
            if (arraySize > 3) 
		  {
                primes[found++] = 3;
            }
            for (int n = 5; n <= arraySize; n += 2)
		  {
                if (isPrime[n]) 
			 {
                    primes[found++] = n;
                }
             }
            return Arrays.copyOf(primes, found);
       }
    }
}

//END OF CODE