Skip to content

Commit

Permalink
Merge pull request #143 from bhutianimukul/main
Browse files Browse the repository at this point in the history
added code for primes and sieve of eranthosis
  • Loading branch information
aman-raza authored Oct 3, 2021
2 parents 75ced1b + e3aded6 commit 5996810
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions prime.cpp
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);
}

0 comments on commit 5996810

Please sign in to comment.