C Program to Generate Prime Numbers Using Sieve of Sundaram

This is a C Program to generate prime numbers within a given range using Sieve of Sundaram.

Here is source code of the C Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2.  
  3. int main() {
  4.     int arraySize, i, j, x;
  5.     int numberPrimes = 0;
  6.     printf("Input a positive integer to find all the prime numbers up to and including that number: ");
  7.     scanf("%d", &arraySize);
  8.     int n = arraySize / 2;
  9.     int size;
  10.     /* array to start off with that will eventually get
  11.      all the composite numbers removed and the remaining
  12.      ones output to the screen                        */
  13.  
  14.     int isPrime[arraySize + 1];
  15.     int TheseArePrime = 0;
  16.  
  17.     for (i = 0; i < n; ++i) {
  18.         isPrime[i] = i;
  19.     }
  20.  
  21.     for (i = 1; i < n; i++) {
  22.         for (j = i; j <= (n - i) / (2 * i + 1); j++) {
  23.             isPrime[i + j + 2 * i * j] = 0;/*From this list, remove all
  24.              numbers of the form i + j + 2ij    */
  25.         }
  26.     }
  27.  
  28.     if (arraySize > 2) {
  29.         isPrime[TheseArePrime++] = 2;/*this IF statement adds 2 to the output     */
  30.     }
  31.  
  32.     for (i = 1; i < n; i++) {
  33.         if (isPrime[i] != 0) {
  34.             isPrime[TheseArePrime++] = i * 2 + 1;
  35.         }
  36.     }
  37.  
  38.     size = sizeof isPrime / sizeof(int);//total size of array/size of array data type
  39.  
  40.     for (x = 0; x <= size; x++) {
  41.         if (isPrime[x] != 0) {
  42.             printf("%d \t", isPrime[x]);
  43.             numberPrimes++;// the counter of the number of primes found
  44.         } else {
  45.             break;
  46.         }
  47.     }
  48.  
  49.     printf("\nNumber of Primes: %d", numberPrimes);
  50.     return 0;
  51. }

Output:

$ gcc SeiveSundaram.c
$ ./a.out
 
Input a positive integer to find all the prime numbers up to and including that number: 27
2 	3 	5 	7 	11 	13 	17 	19 	23 	29 	
Number of Primes: 10

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

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.