Hi when I am trying to submit this code on SPOJ https://www.spoj.com/problems/PRIME1/ I am getting a runtime error

#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
vector primes;
int p[N] = {0};
void seive()
{
for (int i = 2; i <= N; i++)
{
if (p[i] == 0)
{
primes.push_back(i);
//int s = i * i;
for (int j = i; j <= N; j += i)
{
p[j] = 1;
}
}
}
}

int main()
{

seive();
int t;
cin >> t;
while (t--)
{
	int n, m;
	cin >> m >> n;
	bool segment[n - m + 1];
	for (int i = 0; i < n - m + 1; i++)
	{
		segment[i] = 0;
	}

	//Segmented Seive Starts
	for (auto x : primes)
	{
		if (x * x > n)
		{
			break;
		}
		int start = (m / x) * x;
		//Mark all multiples of x as not primes
		if (x >= m and x <= n)
		{
			start = x * 2;
		}
		for (int i = start; i <= n; i += x)
		{
			segment[i - m] = 1;
		}

	}
	for (int i = m; i <= n; i++)
	{
		if (segment[i - m] == 0 && i != 1)
		{
			cout << i << " ";
		}
	}
	cout << endl;
}

}

plz share link of your code

save at http://ide.codingblocks.com/
and then the link generated

Hi,

if(start<m) start+=x;
add this line before marking segment array
because if i<m then segment[i-m] will give error as you are accessing negative index

Hi Actually I added that line but it is still giving runtime error.

Try to make these changes:

  1. first take m then n
  2. use long long int

Hi
If you notice in the above code I have taken m before n.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.