diff --git a/cpp/a.out b/cpp/a.out new file mode 100755 index 0000000..75b8e73 Binary files /dev/null and b/cpp/a.out differ diff --git a/cpp/sieve.cpp b/cpp/sieve.cpp index 00be1c7..f63283d 100644 --- a/cpp/sieve.cpp +++ b/cpp/sieve.cpp @@ -7,7 +7,8 @@ void SieveOfEratosthenes(int n) // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. - bool prime[n + 1]; + vector prime(n + 1, 1); + prime[0]=prime[1]=0; for(int p = 2; p*p <= n; p++) { @@ -15,7 +16,7 @@ void SieveOfEratosthenes(int n) if(prime[p] == true) { // Update all multiples of p - for(int i = p*2; i <= n; i += p) + for(int i = p*p; i <= n; i += p) prime[i] = false; } } @@ -39,4 +40,4 @@ int main() Output :- ( For n = 20 ) Following are the prime numbers smaller than or equal to 20 2 3 5 7 11 13 17 19 -*/ \ No newline at end of file +*/