[Shootout-list] nsieve g++ c++

Daniel Skiles Daniel Skiles <dskiles@gmail.com>
Sat, 19 Mar 2005 14:16:24 -0500


------=_Part_2065_17049054.1111259784190
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hey all,
Here is the nsieve g++ c++ test, written with dev c++ on win32.  Let
me know if there's anything wrong with it.

------=_Part_2065_17049054.1111259784190
Content-Type: text/plain; name="nsieve.cpp"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="nsieve.cpp"

#include <cstdlib>
#include <iostream>

using namespace std;

int nsieve(int, bool[]);

int main(int argc, char *argv[])
{
    int n =3D (argc =3D=3D 2) ? atoi(argv[1]) : 2;
    int m;
    bool* flags;
   =20
    m =3D (1<<n)*10000;
    flags =3D new bool[m];
    cout << "Primes up to " << m << " " << nsieve(m, flags) << endl;
    delete flags;
    flags =3D NULL;
   =20
    m =3D (1<<n-1)*10000;
    flags =3D new bool[m];
    cout << "Primes up to " << m << " " << nsieve(m, flags) << endl;
    delete flags;
    flags =3D NULL;
   =20
    m =3D (1<<n-2)*10000;
    flags =3D new bool[m];
    cout << "Primes up to " << m << " " << nsieve(m, flags) << endl;
    delete flags;
    flags =3D NULL;
   =20
    return EXIT_SUCCESS;
}

int nsieve(int m, bool *isPrime)
{
    int count =3D 0;
   =20
    for (int i=3D2; i<=3Dm; i++)
        isPrime[i] =3D true;
       =20
    for(int i=3D2; i<=3Dm; i++){
        if(isPrime[i]){
            for(int k=3Di*2; k<=3Dm; k+=3Di){
                isPrime[k] =3D false;
            }
            count++;
        }
    }
    return count;
}

------=_Part_2065_17049054.1111259784190--