I learned about Sieve of Sieve of Eratosthenes to find prime numbers between any two given numbers from a video from mycodeschool. Now when I submit the solution, the judge gives me SIGSEGV error.
Code:
// URL - http://www.spoj.com/problems/PRIME1/
// Status - Wrong Answer
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
ll first, n;
cin >> first >> n;
ll primes[n+1];
for (ll i = 2; i <= n; ++i)
primes[i] = 1;
for (ll i = 2; i <= floor(sqrt(n)); ++i) {
if (primes[i] == 1) {
for (ll j = 2; (i*j) <= n; ++j) {
primes[i*j] = 0;
}
}
}
for (ll i = 2; i <= n; ++i) {
if (primes[i] == 1 && i >= first) {
cout << i << '\n';
}
}
}
return 0;
}
Can anyone tell me what is the problem in my code?
Thanks in advance.