My code works perfectly if the seive(n) is 1000000. If seive(n) is more than 10000000, it shows me segmentation fault (core dumped).I've read about segmentation fault. But i couldn't solve with that.
#include <stdio.h> #include <math.h> using namespace std; int seive(long int n) { long int A[n]; for (long int i = 2; i <= n; i += 1) { A[i] = i; } long int root = (int)sqrt(n); for(int j = 2; j <= root; j++) { for(int k = 2*j; k < n ; k = k + j) { A[k] = 0; } } int count = 0; for (int l = 2; l < n; ++l) { if(A[l] != 0) count++; } printf("%d\n", count); } int main() { seive( 1000000); }