forked from rohitsh16/CP_mathematics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70cd525
commit ca4c213
Showing
1 changed file
with
28 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,28 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void SieveOfEratosthenes(int n) | ||
{ | ||
bool prime[n+1]; | ||
memset(prime, true, sizeof(prime)); | ||
|
||
for (int p=2; p*p<=n; p++) | ||
{ | ||
if (prime[p] == true) | ||
{ | ||
for (int i=p*p; i<=n; i += p) | ||
prime[i] = false; | ||
} | ||
} | ||
for (int p=2; p<=n; p++) | ||
if (prime[p]) | ||
cout << p << " "; | ||
} | ||
|
||
int main() | ||
{ | ||
int n = 30; | ||
cout << "Following are the prime numbers smaller than or equal to " << n << endl; | ||
SieveOfEratosthenes(n); | ||
return 0; | ||
} |