# New Algorithm to Generate Prime Numbers from 1 to Nth Number

Apart from Sieve of Eratosthenes method to generate Prime numbers, we can implement a new Algorithm for generating prime numbers from **1** to **N**.

It might be amazing to know that **all the prime numbers ≥ 5** can be traced from a pattern:

Let’s try to understand the series:

Series 1:

5 + 6 = 11

11 + 6 = 17

17 + 6 = 23

23 + 6 = 29

…

…

Series 2:

7 + 6 = 13

13 + 6 = 19

…

…

We see that adding **6** to **5** and again adding **6** to the obtained result, we are able to get another prime number. Similarly, adding **6** to **7** and again adding **6** to the obtained result. we can get another prime number. Hence, we can inference that we can obtain all the primes numbers doing same procedure.

However, when we keep adding **6** to the previous result to obtain next prime number, we can notice some numbers that are not prime numbers.

For example:

13 + 6 = 19 (Prime Number)

19 + 6 = 25 (Composite Number; Say Pseudo Prime Number)

29 + 6 = 35 (Composite Number; Say Pseudo Prime Number)

We may say these types of Composite Numbers as Pseudo Prime Numbers (appear as prime number, but are not in reality).

Now, if we are able to separate these Pseudo Prime Numbers from Real Prime Numbers, we can get the Real Prime Numbers. Using 4 important equations, we can exactly track all these Pseudo Prime Numbers and separate them from Real Prime Numbers.

The Equation that will generate series is:

y = 6 * x ± 1

where x = 1, 2, 3, 4, 5, 6, 7, …

For example:

6 * (1) – 1 = 5 (Prime Number)

6 * (1) + 1 = 7 (Prime Number)

6 * (2) – 1 = 11 (Prime Number)

6 * (2) + 1 = 13 (Prime Number)

6 * (3) – 1 = 17 (Prime Number)

6 * (3) + 1 = 19 (Prime Number)

6 * (4) – 1 = 23 (Prime Number)

6 * (4) + 1 = 25 (Pseudo Prime Number)

We can track all the Pseudo Prime Numbers using 4 equations:

**For the series produced from y = 6 * x – 1:**

**y = (6 * d * x) + d**where, x = {1, 2, 3, 4, 5, 6, …}**y = (6 * d * x) + (d * d)**where, x = {0, 1, 2, 3, 4, 5, …} and d = {5, (5+6), (5+6+6), (5+6+6+6), …, (5+(n-1)*6)}

**For the series produced from y = 6 * x + 1:**

**y = (6 * d * x) + (d * d)**where, x = {0, 1, 2, 3, …}**y = (6 * d * x) + (d * d) – 2 * d**where, x = {1, 2, 3, 4, …}, d = {7, (7+6), (7+6+6), …, (7 + (n – 1) * 6)} and n = {1, 2, 3, 4, 5, …}

Examples

Take d = 5 and x = 0 in equation 2,

y = 25 (Pseudo Prime Number)

Take d = 5 and x = 1 in equation 1,

y = 35 (Pseudo Prime Number)

Similarly, putting values in equation 3 and 4 we can track all Pseudo Prime Numbers.

**How to implement in programming?**

We assume two bool type arrays of same size.

Say first one isarray1, every element of which is initialized to0.

And, second one isarray2, every element of which is initialized to1.

Now,

Inarray1, we initialize the specified indexes with1, the index values are calculated using the equationy = (6 * x) ± 1.

And, inarray2, we initialize the specified indexes with0, the index values are calculated using 4 equations described above.Now, run a loop from

0toNand print the index value of array,

Ifarray1[i] = 1andarray2[i] = 1(i is a prime number).

Below is the implementation of the approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of prime numbers <= n ` `int` `countPrimesUpto(` `int` `n) ` `{ ` ` ` ` ` `// To store the count of prime numbers ` ` ` `int` `count = 0; ` ` ` ` ` `// To mark all the prime numbers according ` ` ` `// to the equation (y = 6*x -/+ 1) ` ` ` `// where x = 1, 2, 3, 4, 5, 6, 7, ... ` ` ` `bool` `arr1[n + 1]; ` ` ` ` ` `// To mark all the Pseudo Prime Numbers ` ` ` `// using the four equations ` ` ` `// described in the approach ` ` ` `bool` `arr2[n + 1]; ` ` ` ` ` `// Starting with >= 5 ` ` ` `int` `d = 5; ` ` ` ` ` `// 2 and 3 are primes ` ` ` `arr1[2] = arr2[2] = 1; ` ` ` `arr1[3] = arr2[3] = 1; ` ` ` ` ` `// Initialize every element of arr1 with 0 ` ` ` `memset` `(arr1, 0, ` `sizeof` `(arr1)); ` ` ` ` ` `// Initialize every element of arr2 with 1 ` ` ` `memset` `(arr2, 1, ` `sizeof` `(arr2)); ` ` ` ` ` `// Update arr1[] to mark all the primes ` ` ` `while` `(d <= n) { ` ` ` ` ` `// For 5, (5 + 6), (5 + 6 + 6), ... ` ` ` `memset` `(arr1 + d, 1, (` `sizeof` `(arr1)) / (n + 1)); ` ` ` ` ` `// For 7, (7 + 6), (7 + 6 + 6), ... ` ` ` `memset` `(arr1 + (d + 2), 1, (` `sizeof` `(arr1)) / (n + 1)); ` ` ` ` ` `// Increase d by 6 ` ` ` `d = d + 6; ` ` ` `} ` ` ` ` ` `// Update arr2[] to mark all pseudo primes ` ` ` `for` `(` `int` `i = 5; i * i <= n; i = i + 6) { ` ` ` `int` `j = 0; ` ` ` ` ` `// We will run while loop until we find all ` ` ` `// pseudo prime numbers <= n ` ` ` `while` `(1) { ` ` ` `int` `flag = 0; ` ` ` ` ` `// Equation 1 ` ` ` `int` `temp1 = 6 * i * (j + 1) + i; ` ` ` ` ` `// Equation 2 ` ` ` `int` `temp2 = ((6 * i * j) + i * i); ` ` ` ` ` `// Equation 3 ` ` ` `int` `temp3 = ((6 * (i + 2) * j) ` ` ` `+ ((i + 2) * (i + 2))); ` ` ` ` ` `// Equation 4 ` ` ` `int` `temp4 = ((6 * (i + 2) * (j + 1)) ` ` ` `+ ((i + 2) * (i + 2)) - 2 * (i + 2)); ` ` ` ` ` `// If obtained pseudo prime number <=n then its ` ` ` `// corresponding index in arr2 is set to 0 ` ` ` ` ` `// Result of equation 1 ` ` ` `if` `(temp1 <= n) { ` ` ` `arr2[temp1] = 0; ` ` ` `} ` ` ` `else` `{ ` ` ` `flag++; ` ` ` `} ` ` ` ` ` `// Result of equation 2 ` ` ` `if` `(temp2 <= n) { ` ` ` `arr2[temp2] = 0; ` ` ` `} ` ` ` `else` `{ ` ` ` `flag++; ` ` ` `} ` ` ` ` ` `// Result of equation 3 ` ` ` `if` `(temp3 <= n) { ` ` ` `arr2[temp3] = 0; ` ` ` `} ` ` ` `else` `{ ` ` ` `flag++; ` ` ` `} ` ` ` ` ` `// Result of equation 4 ` ` ` `if` `(temp4 <= n) { ` ` ` `arr2[temp4] = 0; ` ` ` `} ` ` ` `else` `{ ` ` ` `flag++; ` ` ` `} ` ` ` ` ` `if` `(flag == 4) { ` ` ` `break` `; ` ` ` `} ` ` ` `j++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Include 2 ` ` ` `if` `(n >= 2) ` ` ` `count++; ` ` ` ` ` `// Include 3 ` ` ` `if` `(n >= 3) ` ` ` `count++; ` ` ` ` ` `// If arr1[i] = 1 && arr2[i] = 1 then i is prime number ` ` ` `// i.e. it is a prime which is not a pseudo prime ` ` ` `for` `(` `int` `p = 5; p <= n; p = p + 6) { ` ` ` `if` `(arr2[p] == 1 && arr1[p] == 1) ` ` ` `count++; ` ` ` ` ` `if` `(arr2[p + 2] == 1 && arr1[p + 2] == 1) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 100; ` ` ` `cout << countPrimesUpto(n); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

25

## Recommended Posts:

- Algorithm to generate positive rational numbers
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Represent a number as a sum of maximum possible number of Prime Numbers
- Express an odd number as sum of prime numbers
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Check if a number can be written as a sum of 'k' prime numbers
- Number of distinct prime factors of first n natural numbers
- Find a sequence of N prime numbers whose sum is a composite number
- Print prime numbers with prime sum of digits in an array
- Generate all palindromic numbers less than n
- Pollard's Rho Algorithm for Prime Factorization

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.