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 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
#include <bits/stdc++.h>
using namespace std;
int countPrimesUpto( int n)
{
int count = 0;
bool arr1[n + 1];
bool arr2[n + 1];
int d = 5;
arr1[2] = arr2[2] = 1;
arr1[3] = arr2[3] = 1;
memset (arr1, 0, sizeof (arr1));
memset (arr2, 1, sizeof (arr2));
while (d <= n) {
memset (arr1 + d, 1, ( sizeof (arr1)) / (n + 1));
memset (arr1 + (d + 2), 1, ( sizeof (arr1)) / (n + 1));
d = d + 6;
}
for ( int i = 5; i * i <= n; i = i + 6) {
int j = 0;
while (1) {
int flag = 0;
int temp1 = 6 * i * (j + 1) + i;
int temp2 = ((6 * i * j) + i * i);
int temp3 = ((6 * (i + 2) * j)
+ ((i + 2) * (i + 2)));
int temp4 = ((6 * (i + 2) * (j + 1))
+ ((i + 2) * (i + 2)) - 2 * (i + 2));
if (temp1 <= n) {
arr2[temp1] = 0;
}
else {
flag++;
}
if (temp2 <= n) {
arr2[temp2] = 0;
}
else {
flag++;
}
if (temp3 <= n) {
arr2[temp3] = 0;
}
else {
flag++;
}
if (temp4 <= n) {
arr2[temp4] = 0;
}
else {
flag++;
}
if (flag == 4) {
break ;
}
j++;
}
}
if (n >= 2)
count++;
if (n >= 3)
count++;
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;
}
int main()
{
int n = 100;
cout << countPrimesUpto(n);
}
|
Java
import java.util.*;
class GFG
{
static int countPrimesUpto( int n)
{
int count = 0 ;
int [] arr1 = new int [n + 1 ];
int [] arr2 = new int [n + 1 ];
int d = 5 ;
arr1[ 2 ] = 1 ;
arr2[ 2 ] = 1 ;
arr1[ 3 ] = 1 ;
arr2[ 3 ] = 1 ;
for ( int i = 0 ; i < arr1.length; i++)
arr1[i] = 0 ;
for ( int i = 0 ; i < arr2.length; i++)
arr2[i] = 1 ;
while (d <= n) {
for ( int i = d; i < arr1.length; i += 6 )
arr1[i] = 1 ;
for ( int i = d + 2 ; i < arr1.length; i += 6 )
arr1[i] = 1 ;
d = d + 6 ;
}
for ( int i = 5 ; i * i <= n; i = i + 6 ) {
int j = 0 ;
while ( true ) {
int flag = 0 ;
int temp1 = 6 * i * (j + 1 ) + i;
int temp2 = (( 6 * i * j) + i * i);
int temp3 = (( 6 * (i + 2 ) * j)
+ ((i + 2 ) * (i + 2 )));
int temp4 = (( 6 * (i + 2 ) * (j + 1 ))
+ ((i + 2 ) * (i + 2 )) - 2 * (i + 2 ));
if (temp1 <= n) {
arr2[temp1] = 0 ;
}
else {
flag++;
}
if (temp2 <= n) {
arr2[temp2] = 0 ;
}
else {
flag++;
}
if (temp3 <= n) {
arr2[temp3] = 0 ;
}
else {
flag++;
}
if (temp4 <= n) {
arr2[temp4] = 0 ;
}
else {
flag++;
}
if (flag == 4 ) {
break ;
}
j++;
}
}
if (n >= 2 )
count++;
if (n >= 3 )
count++;
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;
}
public static void main(String[] args)
{
int n = 100 ;
System.out.println(countPrimesUpto(n));
}
}
|
Python3
def countPrimesUpto(n):
count = 0 ;
arr1 = [ 0 for _ in range (n + 1 )];
arr2 = [ 1 for _ in range (n + 1 )];
d = 5 ;
arr1[ 2 ] = 1
arr2[ 2 ] = 1 ;
arr1[ 3 ] = 1
arr2[ 3 ] = 1 ;
while (d < = n) :
for i in range (d, len (arr1), 6 ):
arr1[i] = 1 ;
for i in range (d + 2 , len (arr1), 6 ):
arr1[i] = 1 ;
d = d + 6 ;
for i in range ( 5 , 1 + int (n * * 0.5 ), 6 ):
j = 0 ;
while ( 1 ) :
flag = 0 ;
temp1 = 6 * i * (j + 1 ) + i;
temp2 = (( 6 * i * j) + i * i);
temp3 = (( 6 * (i + 2 ) * j) + ((i + 2 ) * (i + 2 )));
temp4 = (( 6 * (i + 2 ) * (j + 1 )) + ((i + 2 ) * (i + 2 )) - 2 * (i + 2 ));
if (temp1 < = n):
arr2[temp1] = 0 ;
else :
flag + = 1 ;
if (temp2 < = n) :
arr2[temp2] = 0 ;
else :
flag + = 1 ;
if (temp3 < = n) :
arr2[temp3] = 0 ;
else :
flag + = 1 ;
if (temp4 < = n) :
arr2[temp4] = 0 ;
else :
flag + = 1 ;
if (flag = = 4 ) :
break ;
j + = 1
if (n > = 2 ):
count + = 1
if (n > = 3 ):
count + = 1
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;
n = 100 ;
print (countPrimesUpto(n));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countPrimesUpto( int n)
{
int count = 0;
int [] arr1 = new int [n + 1];
int [] arr2 = new int [n + 1];
int d = 5;
arr1[2] = 1;
arr2[2] = 1;
arr1[3] = 1;
arr2[3] = 1;
for ( int i = 0; i < arr1.Length; i++)
arr1[i] = 0;
for ( int i = 0; i < arr2.Length; i++)
arr2[i] = 1;
while (d <= n) {
for ( int i = d; i < arr1.Length; i += 6)
arr1[i] = 1;
for ( int i = d + 2; i < arr1.Length; i += 6)
arr1[i] = 1;
d = d + 6;
}
for ( int i = 5; i * i <= n; i = i + 6) {
int j = 0;
while ( true ) {
int flag = 0;
int temp1 = 6 * i * (j + 1) + i;
int temp2 = ((6 * i * j) + i * i);
int temp3 = ((6 * (i + 2) * j)
+ ((i + 2) * (i + 2)));
int temp4 = ((6 * (i + 2) * (j + 1))
+ ((i + 2) * (i + 2)) - 2 * (i + 2));
if (temp1 <= n) {
arr2[temp1] = 0;
}
else {
flag++;
}
if (temp2 <= n) {
arr2[temp2] = 0;
}
else {
flag++;
}
if (temp3 <= n) {
arr2[temp3] = 0;
}
else {
flag++;
}
if (temp4 <= n) {
arr2[temp4] = 0;
}
else {
flag++;
}
if (flag == 4) {
break ;
}
j++;
}
}
if (n >= 2)
count++;
if (n >= 3)
count++;
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;
}
public static void Main( string [] args)
{
int n = 100;
Console.WriteLine(countPrimesUpto(n));
}
}
|
Javascript
function countPrimesUpto(n)
{
let count = 0;
let arr1 = new Array(n + 1).fill(0);
let arr2 = new Array(n + 1).fill(1);
let d = 5;
arr1[2] = arr2[2] = 1;
arr1[3] = arr2[3] = 1;
while (d <= n) {
for ( var i = d; i < arr1.length; i += 6)
arr1[i] = 1;
for ( var i = d + 2; i < arr1.length; i += 6)
arr1[i] = 1;
d = d + 6;
}
for ( var i = 5; i * i <= n; i = i + 6) {
var j = 0;
while (1) {
var flag = 0;
var temp1 = 6 * i * (j + 1) + i;
var temp2 = ((6 * i * j) + i * i);
var temp3 = ((6 * (i + 2) * j)
+ ((i + 2) * (i + 2)));
var temp4 = ((6 * (i + 2) * (j + 1))
+ ((i + 2) * (i + 2)) - 2 * (i + 2));
if (temp1 <= n) {
arr2[temp1] = 0;
}
else {
flag++;
}
if (temp2 <= n) {
arr2[temp2] = 0;
}
else {
flag++;
}
if (temp3 <= n) {
arr2[temp3] = 0;
}
else {
flag++;
}
if (temp4 <= n) {
arr2[temp4] = 0;
}
else {
flag++;
}
if (flag == 4) {
break ;
}
j++;
}
}
if (n >= 2)
count++;
if (n >= 3)
count++;
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;
}
let n = 100;
console.log(countPrimesUpto(n));
|
Auxiliary Space: O(n)
Last Updated :
01 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...