Open In App

New Algorithm to Generate Prime Numbers from 1 to Nth Number

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

  1. y = (6 * d * x) + d where, x = {1, 2, 3, 4, 5, 6, …}
  2. 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:

  1. y = (6 * d * x) + (d * d) where, x = {0, 1, 2, 3, …}
  2. 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 is array1, every element of which is initialized to 0. And, second one is array2, every element of which is initialized to 1. Now, In array1, we initialize the specified indexes with 1, the index values are calculated using the equation y = (6 * x) ± 1. And, in array2, we initialize the specified indexes with 0, the index values are calculated using 4 equations described above. Now, run a loop from 0 to N and print the index value of array, If array1[i] = 1 and array2[i] = 1 (i is a prime number).

Below is the implementation of the approach: 

CPP




// 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);
}


Java




// Java implementation of the approach
import java.util.*;
class GFG
{
 
  // Function to return the count of prime numbers <= n
  static 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, ...
    int[] arr1 = new int[n + 1];
 
    // To mark all the Pseudo Prime Numbers
    // using the four equations
    // described in the approach
    int[] arr2 = new int[n + 1];
 
    // Starting with >= 5
    int d = 5;
 
    // 2 and 3 are primes
    arr1[2] = 1;
    arr2[2] = 1;
    arr1[3] = 1;
    arr2[3] = 1;
 
    // Initialize every element of arr1 with 0
    for (int i = 0; i < arr1.length; i++)
      arr1[i] = 0;
 
    // Initialize every element of arr2 with 1
    for (int i = 0; i < arr2.length; i++)
      arr2[i] = 1;
 
    // Update arr1[] to mark all the primes
    while (d <= n) {
 
      // For 5, (5 + 6), (5 + 6 + 6), ...
      for (int i = d; i < arr1.length; i += 6)
        arr1[i] = 1;
 
      // For 7, (7 + 6), (7 + 6 + 6), ...
      for (int i = d + 2; i < arr1.length; i += 6)
        arr1[i] = 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 (true) {
        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
  public static void main(String[] args)
  {
    int n = 100;
    System.out.println(countPrimesUpto(n));
  }
}
 
// This code is contributed by phasing17


Python3




# JavaScript implementation of the approach
 
# Function to return the count of prime numbers <= n
def countPrimesUpto(n):
     
    # To store the count of prime numbers
    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, ...
    arr1 = [0 for _ in range(n + 1)];
 
    # To mark all the Pseudo Prime Numbers
    # using the four equations
    # described in the approach
    arr2 = [1 for _ in range(n + 1)];
 
    # Starting with >= 5
    d = 5;
 
    # 2 and 3 are primes
    arr1[2] = 1
    arr2[2] = 1;
    arr1[3] = 1
    arr2[3] = 1;
 
 
    # Update arr1[] to mark all the primes
    while (d <= n) :
 
        # For 5, (5 + 6), (5 + 6 + 6), ...
        for i in range(d, len(arr1), 6):
            arr1[i] = 1;
             
        # For 7, (7 + 6), (7 + 6 + 6), ...
        for i in range(d + 2, len(arr1), 6):
            arr1[i] = 1;
 
        # Increase d by 6
        d = d + 6;
     
 
    # Update arr2[] to mark all pseudo primes
    for i in range(5, 1 + int(n ** 0.5), 6):
        j = 0;
 
        # We will run while loop until we find all
        # pseudo prime numbers <= n
        while (1) :
            flag = 0;
 
            # Equation 1
            temp1 = 6 * i * (j + 1) + i;
 
            # Equation 2
            temp2 = ((6 * i * j) + i * i);
 
            # Equation 3
            temp3 = ((6 * (i + 2) * j) + ((i + 2) * (i + 2)));
 
            # Equation 4
            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 += 1;
             
 
            # Result of equation 2
            if (temp2 <= n) :
                arr2[temp2] = 0;
             
            else :
                flag += 1;
             
 
            # Result of equation 3
            if (temp3 <= n) :
                arr2[temp3] = 0;
             
            else :
                flag += 1;
             
 
            # Result of equation 4
            if (temp4 <= n) :
                arr2[temp4] = 0;
             
            else :
                flag += 1;
             
 
            if (flag == 4) :
                break;
             
            j += 1
         
    # Include 2
    if (n >= 2):
        count +=1
 
    # Include 3
    if (n >= 3):
        count += 1
 
    # 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 p in range(5, n + 1, 6):
        if (arr2[p] == 1 and arr1[p] == 1):
            count += 1
 
        if arr2[p + 2] == 1 and arr1[p + 2] == 1:
            count += 1
     
    return count;
 
# Driver code
n = 100;
print(countPrimesUpto(n));
 
# This code is contributed by phasing17


C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to return the count of prime numbers <= n
  static 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, ...
    int[] arr1 = new int[n + 1];
 
    // To mark all the Pseudo Prime Numbers
    // using the four equations
    // described in the approach
    int[] arr2 = new int[n + 1];
 
    // Starting with >= 5
    int d = 5;
 
    // 2 and 3 are primes
    arr1[2] = 1;
    arr2[2] = 1;
    arr1[3] = 1;
    arr2[3] = 1;
 
    // Initialize every element of arr1 with 0
    for (int i = 0; i < arr1.Length; i++)
      arr1[i] = 0;
 
    // Initialize every element of arr2 with 1
    for (int i = 0; i < arr2.Length; i++)
      arr2[i] = 1;
 
    // Update arr1[] to mark all the primes
    while (d <= n) {
 
      // For 5, (5 + 6), (5 + 6 + 6), ...
      for (int i = d; i < arr1.Length; i += 6)
        arr1[i] = 1;
 
      // For 7, (7 + 6), (7 + 6 + 6), ...
      for (int i = d + 2; i < arr1.Length; i += 6)
        arr1[i] = 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 (true) {
        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
  public static void Main(string[] args)
  {
    int n = 100;
    Console.WriteLine(countPrimesUpto(n));
  }
}
 
// This code is contributed by phasing17


Javascript




// JavaScript implementation of the approach
 
 
// Function to return the count of prime numbers <= n
function countPrimesUpto(n)
{
 
    // To store the count of prime numbers
    let 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, ...
    let arr1 = new Array(n + 1).fill(0);
 
    // To mark all the Pseudo Prime Numbers
    // using the four equations
    // described in the approach
    let arr2 = new Array(n + 1).fill(1);
 
    // Starting with >= 5
    let d = 5;
 
    // 2 and 3 are primes
    arr1[2] = arr2[2] = 1;
    arr1[3] = arr2[3] = 1;
 
 
    // Update arr1[] to mark all the primes
    while (d <= n) {
 
        // For 5, (5 + 6), (5 + 6 + 6), ...
        for (var i = d; i < arr1.length; i += 6)
            arr1[i] = 1;
             
        // For 7, (7 + 6), (7 + 6 + 6), ...
        for (var i = d + 2; i < arr1.length; i += 6)
            arr1[i] = 1;
 
        // Increase d by 6
        d = d + 6;
    }
 
    // Update arr2[] to mark all pseudo primes
    for (var i = 5; i * i <= n; i = i + 6) {
        var j = 0;
 
        // We will run while loop until we find all
        // pseudo prime numbers <= n
        while (1) {
            var flag = 0;
 
            // Equation 1
            var temp1 = 6 * i * (j + 1) + i;
 
            // Equation 2
            var temp2 = ((6 * i * j) + i * i);
 
            // Equation 3
            var temp3 = ((6 * (i + 2) * j)
                        + ((i + 2) * (i + 2)));
 
            // Equation 4
            var 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 (var 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
let n = 100;
console.log(countPrimesUpto(n));
 
 
// This code is contributed by phasing17


Output

25

Auxiliary Space: O(n)



Last Updated : 01 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads