# C++ Program to Implement Wheel Sieve to Generate Prime Numbers

«
»
This is a C++ Program to find prime number between a given range using Wheel Seive method. Wheel factorization is a graphical method for manually performing a preliminary to the Sieve of Eratosthenes that separates prime numbers from composites. Start by writing the natural numbers around circles as shown below. Prime numbers in the innermost circle have their multiples in similar positions as themselves in the other circles, forming spokes of primes and their multiples. Multiples of the prime numbers in the innermost circle form spokes of composite numbers in the outer circles.

Here is source code of the C++ Program to Implement wheel Sieve 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 <iostream>`
2. `#include <math.h>`
3. `#include <stdlib.h>`
4. ` `
5. `using namespace std;`
6. ` `
7. `#define MAX_NUM 50`
8. `// array will be initialized to 0 being global`
9. `int primes[MAX_NUM];`
10. ` `
11. `void gen_sieve_primes(void)`
12. `{`
13. `    for (int p = 2; p < MAX_NUM; p++) // for all elements in array`
14. `    {`
15. `        if (primes[p] == 0) // it is not multiple of any other prime`
16. `            primes[p] = 1; // mark it as prime`
17. ` `
18. `        // mark all multiples of prime selected above as non primes`
19. `        int c = 2;`
20. `        int mul = p * c;`
21. `        for (; mul < MAX_NUM;)`
22. `        {`
23. `            primes[mul] = -1;`
24. `            c++;`
25. `            mul = p * c;`
26. `        }`
27. `    }`
28. `}`
29. ` `
30. `void print_all_primes()`
31. `{`
32. `    int c = 0;`
33. `    for (int i = 0; i < MAX_NUM; i++)`
34. `    {`
35. `        if (primes[i] == 1)`
36. `        {`
37. `            c++;`
38. ` `
39. `            if (c < 4)`
40. `            {`
41. `                switch (c)`
42. `                {`
43. `                    case 1:`
44. `                        cout << c << "st prime is: " << i << endl;`
45. `                        break;`
46. `                    case 2:`
47. `                        cout << c << "nd prime is: " << i << endl;`
48. `                        break;`
49. `                    case 3:`
50. `                        cout << c << "rd prime is: " << i << endl;`
51. `                        break;`
52. ` `
53. `                    default:`
54. `                        break;`
55. `                }`
56. `            }`
57. ` `
58. `            else`
59. `                cout << c << "th prime is: " << i << endl;`
60. `        }`
61. `    }`
62. `}`
63. ` `
64. `int main()`
65. `{`
66. `    gen_sieve_primes();`
67. `    print_all_primes();`
68. `    return 0;`
69. `}`

Output:

```\$ g++ WheelSeive.cpp
\$ a.out

1st prime is: 2
2nd prime is: 3
3rd prime is: 5
4th prime is: 7
5th prime is: 11
6th prime is: 13
7th prime is: 17
8th prime is: 19
9th prime is: 23
10th prime is: 29
11th prime is: 31
12th prime is: 37
13th prime is: 41
14th prime is: 43
15th prime is: 47```

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

Note: Join free Sanfoundry classes at Telegram or Youtube