Java Program to Implement Sieve of Atkin to Generate Prime Numbers

This is a Java Program to Implement Sieve Of Atkin Algorithm. This is a program to find all primes less than a number.

Here is the source code of the Java Program to Implement Sieve Of Atkin Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to implement Sieve Of Atkin Prime generation
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class SieveOfAtkin **/
  8. public class  SieveOfAtkin
  9. {
  10.     /** Function to calculate all primes less than n **/
  11.     private boolean[] calcPrimes(int limit)
  12.     {
  13.         /** initialize the sieve **/
  14.         boolean[] prime = new boolean[limit + 1];
  15.         prime[2] = true;
  16.         prime[3] = true;
  17.         int root = (int) Math.ceil(Math.sqrt(limit));
  18.  
  19.         /** put in candidate primes: 
  20.            integers which have an odd number of
  21.            representations by certain quadratic forms **/
  22.         for (int x = 1; x < root; x++)
  23.         {
  24.             for (int y = 1; y < root; y++)
  25.             {
  26.                 int n = 4 * x * x + y * y;
  27.                 if (n <= limit && (n % 12 == 1 || n % 12 == 5))
  28.                     prime[n] = !prime[n];
  29.                 n = 3 * x * x + y * y;
  30.                 if (n <= limit && n % 12 == 7)
  31.                     prime[n] = !prime[n];
  32.                 n = 3 * x * x - y * y;
  33.                 if ((x > y) && (n <= limit) && (n % 12 == 11))
  34.                     prime[n] = !prime[n];
  35.             }
  36.         }
  37.         /** eliminate composites by sieving, omit multiples of its square **/
  38.         for (int i = 5; i <= root; i++)
  39.             if (prime[i])
  40.                 for (int j = i * i; j < limit; j += i * i)
  41.                     prime[j] = false;
  42.  
  43.         return prime;
  44.     }
  45.     /** Function to get all primes **/
  46.     public void getPrimes(int N)
  47.     {
  48.         boolean[] primes = calcPrimes(N);
  49.         display(primes);
  50.     }
  51.     /** Function to display all primes **/
  52.     public void display(boolean[] primes)
  53.     {
  54.         System.out.print("\nPrimes = ");
  55.         for (int i = 2; i < primes.length; i++)
  56.             if (primes[i])
  57.                 System.out.print(i +" ");
  58.         System.out.println();
  59.     }
  60.     /** Main function **/
  61.     public static void main (String[] args) 
  62.     {
  63.         Scanner scan = new Scanner(System.in);
  64.         System.out.println("Sieve Of Atkin Prime Algorithm Test\n");
  65.         /** Make an object of SieveOfAtkin class **/
  66.          SieveOfAtkin soa = new  SieveOfAtkin();
  67.         /** Accept n **/
  68.         System.out.println("Enter number to find all primes less than the number\n");
  69.         int n = scan.nextInt();
  70.         soa.getPrimes(n);        
  71.     }
  72. }

Sieve Of Atkin Prime Algorithm Test
 
Enter number to find all primes less than the number
 
500
 
Primes = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313
317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439
443 449 457 461 463 467 479 487 491 499

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.