-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from Sindhu-321/main
Added Sieve of eratosthenes algorithm
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
#include <vector> | ||
|
||
vector<long long int> soe(long long int a){ | ||
vector<bool> ref(a+1); | ||
vector<long long int> ans; | ||
int i=2; | ||
ans.push_back(2); | ||
while(i<=a){ | ||
for(int j=i*i;j<=a;j+=i){ | ||
if(!ref[j])ref[j]=true; | ||
} | ||
i++; | ||
while(ref[i])i++; | ||
if(i<=a) ans.push_back(i); | ||
|
||
} | ||
return ans; | ||
|
||
} | ||
|
||
int main() | ||
{ | ||
long long int n; | ||
cout<<"enter number"<<endl; | ||
cin>>n; | ||
vector<long long int> primes=soe(n); | ||
cout<<"The primes which are less than or equal to "<<n<<" are:"<<endl; | ||
for(int i=0;i<primes.size();i++)cout<<primes[i]<<" "; | ||
cout<<endl; | ||
|
||
} |