Last active
April 25, 2020 13:10
-
-
Save Remonhasan/d429f2e1fe952d6ea0a8f549b6bc4efd to your computer and use it in GitHub Desktop.
Sieve of Eratosthense [ Find Primes ]
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
/* 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