Skip to content

Commit

Permalink
Added program to generate prime numbers list issue ambujraj#76
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibzz committed Oct 1, 2018
1 parent 110abad commit ead9e89
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions prime/generateListOfPrimes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//Program to generate prime numbers less than or equal to n (n < 100000). This is an O(n) algorithm. This algorithm is called Sieve_of_Eratosthenes (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)

#include<bits/stdc++.h>
#define lli long long int //macros
#define loop(a,b) for(lli i=a;i<b;i++)

using namespace std;
int main()
{
cout<<"Enter value of n: ";
lli n; //thee number till which you want to generate primes
cin>>n;

bool marked[n+1]; //a boolean array to mark the indexes of array. Indexes are treated as numbers. Ex. if marked[2] = 1, this means
//that 2 is a prime number.

loop(2,n+1) //initially mark all numbers till n as non primes.
{
marked[i]=0;
}

lli inc,num,start,i=0;
num=2;
inc=2;
start=2;

while(inc <= n) //this loop takes all the unmarked numbers and mark their multiples as non-primes.
{
inc = start;
num=2*inc;
while(num<=n)
{
if(!marked[num])
marked[num]=1;
num+=inc;
}
i=start+1;
while(marked[i] && i<=n)
{
i++;
}
start=i;
}

i=2;
cout<<"All prime numbers less than equal to "<<n<<": \n";
while(i<=n) //print all primes.
{
if(!marked[i])
cout<<i<<" ";
i++;
}
return 0;
}

0 comments on commit ead9e89

Please sign in to comment.