Skip to content

Commit

Permalink
Merge pull request Harshsngh07#249 from Nandini2901/Nandini2901-patch-1
Browse files Browse the repository at this point in the history
Create sieve.cpp
  • Loading branch information
Harshsngh07 authored Oct 11, 2020
2 parents 571926b + cd81034 commit 142f31b
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 142f31b

Please sign in to comment.