# C Program to Implement Sieve of Atkin to Generate Prime Numbers Between Given Range

«
»
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.

1. `#include <stdio.h>`
2. `#include <math.h>`
3. ` `
4. `int main() {`
5. `    int limit;`
6. `    int wlimit;`
7. `    int i, j, k, x, y, z;`
8. `    unsigned char *sieb;`
9. ` `
10. `    printf("Please insert a number up to which all primes are calculated: ");`
11. `    scanf("%d", &limit);`
12. ` `
13. `    sieb = (unsigned char *) calloc(limit, sizeof(unsigned char));`
14. ` `
15. `    wlimit = sqrt(limit);`
16. ` `
17. `    for (x = 1; x <= wlimit; x++) {`
18. `        for (y = 1; y <= wlimit; y++) {`
19. `            z = 4 * x * x + y * y;`
20. `            if (z <= limit && (z % 60 == 1 || z % 60 == 13 || z % 60 == 17 || z`
21. `                    % 60 == 29 || z % 60 == 37 || z % 60 == 41 || z % 60 == 49`
22. `                    || z % 60 == 53)) {`
23. `                sieb[z] = !sieb[z];`
24. `            }`
25. `            z = 3 * x * x + y * y;`
26. `            if (z <= limit && (z % 60 == 7 || z % 60 == 19 || z % 60 == 31 || z`
27. `                    % 60 == 43)) {`
28. `                sieb[z] = !sieb[z];`
29. `            }`
30. `            z = 3 * x * x - y * y;`
31. `            if (x > y && z <= limit && (z % 60 == 11 || z % 60 == 23 || z % 60`
32. `                    == 47 || z % 60 == 59)) {`
33. `                sieb[z] = !sieb[z];`
34. `            }`
35. `        }`
36. `    }`
37. ` `
38. `    for (i = 5; i <= wlimit; i++) {`
39. `        if (sieb[i] == 1) {`
40. `            for (j = 1; j * i * i <= limit; j++) {`
41. `                sieb[j * i * i] = 0;`
42. `            }`
43. `        }`
44. `    }`
45. ` `
46. `    printf("The following primes have been calculated:\n2\n3\n5");`
47. `    for (i = 5; i <= limit; i++) {`
48. `        if (sieb[i] == 1) {`
49. `            printf("\n%d", i);`
50. `        }`
51. `    }`
52. ` `
53. `    scanf("%d", &i);`
54. `    return 0;`
55. `}`

Output:

advertisement
```\$ 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: Join Sanfoundry@Linkedin

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

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!
advertisement
advertisement
advertisement Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter