-
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 #143 from bhutianimukul/main
added code for primes and sieve of eranthosis
- Loading branch information
Showing
1 changed file
with
41 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,41 @@ | ||
#include <iostream> | ||
#include <cmath> | ||
#include <cstring> | ||
using namespace std; | ||
bool checkPrime(int n) | ||
{ | ||
for (int i = 2; i * i <= n; i++) | ||
{ | ||
if (n % i != 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
void sieveOfEran(int n) | ||
{ // time complexity | ||
// n * log(log(n)) | ||
bool arr[n + 1]; | ||
memset(arr, true, n + 1); | ||
|
||
for (int i = 2; i * i <= n; i++) | ||
{ | ||
cout << " i" << i << ":" << arr[i] << endl; | ||
if (arr[i] == true) | ||
{ | ||
for (int j = 2 * i; j <= n; j = j + i) | ||
{ | ||
arr[j] = false; | ||
} | ||
} | ||
} | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
cout << i << ":" << arr[i]; | ||
cout << "\n"; | ||
} | ||
} | ||
int main() | ||
{ | ||
int n = 12; | ||
sieveOfEran(10); | ||
} |