diff --git a/Algebra/SieveOfEratosthenes.cpp b/Algebra/SieveOfEratosthenes.cpp new file mode 100644 index 0000000..7e7c584 --- /dev/null +++ b/Algebra/SieveOfEratosthenes.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +// Printing all primes in the range [1,n] using Sieve of Eratosthenes +void sieveOfEratosthenes(int n) +{ + vector is_prime(n + 1, true); + is_prime[0] = is_prime[1] = false; + for (int i = 2; i * i <= n; i++) { + if (is_prime[i]) { + for (int j = i * i; j <= n; j += i) + is_prime[j] = false; + } + } + for (int i = 1;i <= n;i++) + { + if (is_prime[i]) + { + cout << i << " is prime." << endl; + } + } +} +int main() +{ + int n; + cin >> n; + sieveOfEratosthenes(n); + return 0; +} \ No newline at end of file