C++ Program to Implement Sieve of Atkins

«
»
This C++ Program demonstrates the implementation of Sieve of Atkins.

Here is source code of the C++ Program to demonstrate the implementation of Sieve of Atkins. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Implement Sieve of Atkins
  3.  */
  4. #include <iostream>
  5. #include <cmath>
  6. #include <vector>
  7. #define ll long long 
  8.  
  9. using namespace std;
  10.  
  11. /* 
  12.  * Sieve of Atkins
  13.  */
  14. void sieve_atkins(ll int n)
  15. {
  16.     vector<bool> is_prime(n + 1);
  17.     is_prime[2] = true;
  18.     is_prime[3] = true;
  19.     for (ll int i = 5; i <= n; i++)
  20.     {
  21.         is_prime[i] = false;
  22.     }
  23.     ll int lim = ceil(sqrt(n));
  24.     for (ll int x = 1; x <= lim; x++)
  25.     {
  26.         for (ll int y = 1; y <= lim; y++)
  27.         {
  28.             ll int num = (4 * x * x + y * y);
  29.             if (num <= n && (num % 12 == 1 || num % 12 == 5))
  30.             {
  31.                 is_prime[num] = true;
  32.             }
  33.             num = (3 * x * x + y * y);
  34.             if (num <= n && (num % 12 == 7))
  35.             {
  36.                 is_prime[num] = true;
  37.             }
  38.  
  39.             if (x > y)
  40.             {
  41.                 num = (3 * x * x - y * y);
  42.                 if (num <= n && (num % 12 == 11))
  43.                 {
  44.                     is_prime[num] = true;
  45.                 }
  46.             }
  47.         }
  48.     }
  49.     for (ll int i = 5; i <= lim; i++)
  50.     {
  51.         if (is_prime[i])
  52.         {
  53.             for (ll int j = i * i; j <= n; j += i)
  54.             {
  55.                 is_prime[j] = false;
  56.             }
  57.         }
  58.     }
  59.  
  60.     for (ll int i = 2; i <= n; i++)
  61.     {
  62.          if (is_prime[i])
  63.          {
  64.              cout<<i<<"\t";
  65.          }
  66.     }
  67. }
  68.  
  69. /* 
  70.  * Main
  71.  */
  72. int main()
  73. {
  74.     ll int n;
  75.     n = 300;
  76.     cout<<"Following are the prime numbers below "<<n<<endl;
  77.     sieve_atkins(n);
  78.     cout<<endl;
  79.     return 0;
  80. }

$ g++ sieve_atkins.cpp
$ a.out
 
Following are the prime numbers below 300
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
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

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 & technical discussions at Telegram SanfoundryClasses.