Created
May 1, 2020 05:00
-
-
Save ksdkamesh99/256de46c18c634c8694fc388d1049355 to your computer and use it in GitHub Desktop.
Implementation of A classical Way of generating the prime numbers between 1 to n using Seive of Erothenus Algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
typedef long long int lli; | |
typedef long long ll; | |
#define fi(i,n) for(int i=0;i<n;i++) | |
#define Fi(i,k,n) for(int i=k;i<n;i++) | |
#define fd(i,n) for(int i=n-1;i>=0;i--) | |
#define Fd(i,k,n) for(int i=n-1;i>=k;i--) | |
#define pop(i) __builtin_popcount(i) | |
const int mod = 1000000007; | |
#define deb(x) cout<<#x<<" "<<x<<"\n" | |
#define PI 3.1415926535897932384626 | |
#define all(x) x.begin(),x.end() | |
#define sortall(x) sort(all(x)) | |
#define mp make_pair | |
typedef pair<int,int> pii; | |
typedef pair<ll,ll> pll; | |
#define F first | |
#define S second | |
#define itr(it,a) for(it=a.begin();it!=a.end();it++) | |
#define bs(a,b) binary_search(all(a),b) | |
#define lb(a,b) lower_bound(all(a),b) | |
#define ub(a,b) upper_bound(all(a),b) | |
#define nod(x) floor(log10(x))+1 | |
#define pb(s) push_back(s) | |
void SieveOfEratosthenes(int n) | |
{ | |
// Create a boolean array "prime[0..n]" and initialize | |
// all entries it as true. A value in prime[i] will | |
// finally be false if i is Not a prime, else true. | |
bool prime[n+1]; | |
memset(prime, true, sizeof(prime)); | |
for (int p=2; p*p<=n; p++) | |
{ | |
// If prime[p] is not changed, then it is a prime | |
if (prime[p] == true) | |
{ | |
// Update all multiples of p greater than or | |
// equal to the square of it | |
// numbers which are multiple of p and are | |
// less than p^2 are already been marked. | |
for (int i=p*p; i<=n; i += p) | |
prime[i] = false; | |
} | |
} | |
// Print all prime numbers | |
for (int p=2; p<=n; p++) | |
if (prime[p]) | |
cout << p << " "; | |
} | |
int main(){ | |
int size=1000000; | |
SieveOfEratosthenes(size); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment