Skip to content

Instantly share code, notes, and snippets.

@Remonhasan
Last active April 25, 2020 13:10
Show Gist options
  • Save Remonhasan/d429f2e1fe952d6ea0a8f549b6bc4efd to your computer and use it in GitHub Desktop.
Save Remonhasan/d429f2e1fe952d6ea0a8f549b6bc4efd to your computer and use it in GitHub Desktop.
Sieve of Eratosthense [ Find Primes ]
/* Author: Remon Hasan
Algorithm: Sieve of Eratosthense
Problem: Find all primes within given range
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void sieve(ll N)
{
bool prime[N+1];
memset(prime,true,sizeof(prime)); // initially all primes
for(int i=2; i*i<=N; i++)
{
if(prime[i]==true)
{
for(int j=i*i; j<=N; j+=i) // false the multiples
{
prime[j]=false;
}
}
}
for(int i=2; i<=N; i++) // print all primes
{
if(prime[i])
cout<<i<<endl;
}
}
int main ()
{
ll a;
cin>>a;
sieve(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment