Eratosthenes Sieve Implementation My friend and I were doing an Eratosthenes Sieve implementation, it works, but I think there is a mistake with "n" and with the first for condition ("i&lt;=n"). I think that condition can cause a segmentation fault as there's no criba[n], am I right? Any suggestion to make it faster?

#include &lt;bits/stdc++.h&gt;

using namespace std;

const long long int n=1000000;
bool criba[n+1];

void gencriba(){
    memset(criba,true,sizeof(criba));
    criba[0]=criba[1]=false;
    for(long long int i=2;i&lt;=n;++i){ ///Aquí es donde usa &lt;=
        if(criba[i]){
            for(long long int j=i;j&lt;=n/i;++j){
                criba[j*i]=false;
            }
        }
    }
}

int main()
{
    gencriba();
    int c;
    cin&gt;&gt;c;
    cout&lt;&lt;criba[c]&lt;&lt;endl;
    return 0;
}


Edit: Decided to change the array size to n+1, so that there's no possibility of getting an error because of invalid memory address
The code looks pretty good, but a few things that can get you into trouble should be addressed:

Don't #include &lt;bits/stdc++.h&gt;. See this answer on Stack Overflow for an explanation of why. TL;DR - it includes everything and you don't need all that it includes. It's just making your compile times take much longer than they should.

There's no need to use long long for values up to and including 1,000,000. A 32-bit long will do just fine with plenty of room in case you go over.

You should avoid global variables. In a program of this size it's no big deal, but it's a bad habit to get into. If your program grows to a larger size, it can become very difficult to reason about why and where a global variable is being changed, making it harder to find and fix bugs or to extend the application's functionality.

Be careful creating such large arrays. In this case, because they are global, they are in your program's data segment rather than on the stack so it's not an issue. However, if you were to create them on the stack in this manner, there is a chance that you could end up with a stack overflow error. 1 million bools will take up at least 1 Megabyte of memory. Many applications have a stack that is smaller than that, or only a little larger by default. 

For readabliity, I recommend using more spaces in your code. In particular, I always leave a space before and after any operator. So your main() function would look more like this:

int main()
{
    gencriba();
    int c;
    cin &gt;&gt; c;
    cout &lt;&lt; criba [ c ] &lt;&lt; endl;
    return 0;
}


It makes it easier to see at a glance what's a variable and how they're being operated on.

In terms of performance, the best optimization is usually a better algorithm. This Stack Overflow Question asks which algorithms are fastest and has several answers.