Skip to content

Commit

Permalink
Create sieve.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nandini2901 authored Oct 10, 2020
1 parent 571926b commit cd81034
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Language/C++/sieve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//Printing the prime numbers upto a given number 'n' in O(n log log n) time.
// as it takes O(n) time in iterartive method.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n + 1];
memset(arr, 0, sizeof(arr));
arr[0] = 1;
arr[1] = 1;
for (int i = 2; i <= n; i++)
{
if (arr[i] == 0)
{
for (int j = 2; j <= n / i; j++)
{
if (arr[i * j] != 1)
{
arr[i * j] = 1;
}
}
}
}
for (int i = 0; i <= n; i++)
{
if (arr[i] == 0)
{
cout << i << " ";
}
}
}

0 comments on commit cd81034

Please sign in to comment.