This is a C Program to find the prime numbers. The sieve of Atkin is a algorithm for finding all prime numbers up to including a specified integer. It is an optimized version of the ancient sieve of Eratosthenes which does some preliminary work and then marks off multiples of the square of each prime, rather than multiples of the prime itself.
Here is source code of the C Program to Implement Sieve of Atkin to Generate Prime Numbers Between Given Range. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include <stdio.h>
#include <math.h>
int main() {
int limit;
int wlimit;
int i, j, k, x, y, z;
unsigned char *sieb;
printf("Please insert a number up to which all primes are calculated: ");
scanf("%d", &limit);
sieb = (unsigned char *) calloc(limit, sizeof(unsigned char));
wlimit = sqrt(limit);
for (x = 1; x <= wlimit; x++) {
for (y = 1; y <= wlimit; y++) {
z = 4 * x * x + y * y;
if (z <= limit && (z % 60 == 1 || z % 60 == 13 || z % 60 == 17 || z
% 60 == 29 || z % 60 == 37 || z % 60 == 41 || z % 60 == 49
|| z % 60 == 53)) {
sieb[z] = !sieb[z];
}
z = 3 * x * x + y * y;
if (z <= limit && (z % 60 == 7 || z % 60 == 19 || z % 60 == 31 || z
% 60 == 43)) {
sieb[z] = !sieb[z];
}
z = 3 * x * x - y * y;
if (x > y && z <= limit && (z % 60 == 11 || z % 60 == 23 || z % 60
== 47 || z % 60 == 59)) {
sieb[z] = !sieb[z];
}
}
}
for (i = 5; i <= wlimit; i++) {
if (sieb[i] == 1) {
for (j = 1; j * i * i <= limit; j++) {
sieb[j * i * i] = 0;
}
}
}
printf("The following primes have been calculated:\n2\n3\n5");
for (i = 5; i <= limit; i++) {
if (sieb[i] == 1) {
printf("\n%d", i);
}
}
scanf("%d", &i);
return 0;
}
Output:
$ gcc SieveAtkin.c $ ./a.out Please insert a number up to which all primes are calculated: 80 The following primes have been calculated: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Related Posts:
- Check C Books
- Check Computer Science Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Apply for C Internship