Prime generator from one to n My code calculates primes from one to n. I have verified that the code always produces all the primes in that range correctly.

Are there any optimizations that I can make? Are there any bad programming practices besides variable names (e.g. l is close to 1)? Any better normal Windows API? I am using the Sieve of Eratosthenes.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;math.h&gt;
#include &lt;Windows.h&gt;

using namespace std;

#define printprimes() //for each(bool b in primes) cout &lt;&lt; b &lt;&lt; endl;

int main(int argc, char* argv[])
{
    const double s = GetTickCount();
    unsigned long long numt;
    if(argc &lt; 2) {
        cout &lt;&lt; "Usage: "&lt;&lt;argv[0]&lt;&lt;" &lt;primes until...&gt;" &lt;&lt; endl;
        return 1;
    }

    else if(atoi(argv[1])&lt;1) {
        cout &lt;&lt; "Usage: "&lt;&lt;argv[0]&lt;&lt;" &lt;primes until...&gt;" &lt;&lt; endl;
        return 1;
    }
    numt = atol(argv[1])+1;

    bool skipprint = false;

    if(argc &gt;=3) if(!strcmp(argv[2], "noprint")) skipprint = true;

    vector&lt;bool&gt; primes(numt);

    primes.assign(numt, true);

    primes[0] = false;
    primes[1] = false;

    long double sqrtt = sqrt(numt);
    for(unsigned long long l = 0; l&lt;=sqrtt; l++) {
        if(!primes[l]) {
            //cout &lt;&lt; l &lt;&lt; " is false" &lt;&lt; endl;
            continue;
        }
        for(unsigned long long cl = 2*l; cl &lt; numt; cl+= l) {
            //cout &lt;&lt; cl &lt;&lt; ", a multiple of " &lt;&lt; l &lt;&lt; endl;
            primes[cl] = false;
        }
    }
    const double m = GetTickCount();
    unsigned long long count = 0;
    if(!skipprint) for(unsigned long long l = 0; l&lt;numt; l++) if(primes[l]) {
        cout &lt;&lt; l &lt;&lt; endl;
        count ++;
    }
    if(skipprint) for(unsigned long long l = 0; l&lt;numt; l++) if(primes[l]) count ++;
    const double e = GetTickCount();
    cout &lt;&lt; endl;
    cout &lt;&lt; count &lt;&lt; " primes less than or equal to " &lt;&lt; numt-1 &lt;&lt; endl;
    cout &lt;&lt; "Calculation took " &lt;&lt; m-s &lt;&lt; " ms";
    if(!skipprint) cout &lt;&lt; " and printing took " &lt;&lt; e-m &lt;&lt; " ms";
    else cout &lt;&lt; " and counting took " &lt;&lt; e-m &lt;&lt; " ms";
    cout &lt;&lt;"." &lt;&lt; endl;
    //for each(bool b in primes) cout &lt;&lt; b &lt;&lt; endl;

    return 0;
}
The algorithm looks good overall. Here are a few comments :



using namespace std; is sometimes frowned upon : some might say that putting it in a cpp file (by opposition to a header file) is ok, some might say that it's not. In any case, it is worth having a read at the link.



#define printprimes() //for each(bool b in primes) cout &lt;&lt; b &lt;&lt; endl;


does not seem really useful.



If you want to handle wrong input in a better way you could have a look at the other ways to convert arrays of char to number.



bool skipprint = false;

if(argc &gt;=3) if(!strcmp(argv[2], "noprint")) skipprint = true;


can become :

bool skipprint = false;
if(argc &gt;=3 &amp;&amp; !strcmp(argv[2], "noprint")) skipprint = true;


which is nothing but :

bool skipprint = (argc &gt;=3 &amp;&amp; !strcmp(argv[2], "noprint"));


Also it might be even better to do :

bool print = (argc &lt; 3 || strcmp(argv[2], "noprint"));




    if(!primes[l]) {
        //cout &lt;&lt; l &lt;&lt; " is false" &lt;&lt; endl;
        continue;
    }
    for(unsigned long long cl = 2*l; cl &lt; numt; cl+= l) {
        //cout &lt;&lt; cl &lt;&lt; ", a multiple of " &lt;&lt; l &lt;&lt; endl;
        primes[cl] = false;
    }


can be written :

    if(primes[l]) {
        for(unsigned long long cl = 2*l; cl &lt; numt; cl+= l) {
            //cout &lt;&lt; cl &lt;&lt; ", a multiple of " &lt;&lt; l &lt;&lt; endl;
            primes[cl] = false;
        }
    }




In for(unsigned long long l = 0; l&lt;=sqrtt; l++), you can start from index 2. Please note that if you forget to initialise prime[0] to false, you'll get stuck in an infinite loop.



In for(unsigned long long cl = 2*l; cl &lt; numt; cl+= l), you can start at index l*l because any i*l with i &lt; l should have been crossed out already.



if(!skipprint) for(unsigned long long l = 0; l&lt;numt; l++) if(primes[l]) {
    cout &lt;&lt; l &lt;&lt; endl;
    count ++;
}
if(skipprint) for(unsigned long long l = 0; l&lt;numt; l++) if(primes[l]) count ++;


could be rewritten in many different ways. Most obvious one is to use an else :

if (skipprint) for(unsigned long long l = 0; l&lt;numt; l++) if(primes[l]) count ++;
else for(unsigned long long l = 0; l&lt;numt; l++) if(primes[l]) {
    cout &lt;&lt; l &lt;&lt; endl;
    count ++;
}


A probably better option in order not to repeat code is to do :

for(unsigned long long l = 0; l&lt;numt; l++) if(primes[l]) {
    if (!skipprint)
        cout &lt;&lt; l &lt;&lt; endl;
    count ++;
}




Also, you should probably write your algorithm in a function on its own. Other optimisation can be analysed like removing all even numbers from the beginning to use a smaller container.