[Shootout-list] nsieve g++ c++ (now with vectors)
Daniel Skiles
Daniel Skiles <dskiles@gmail.com>
Sun, 20 Mar 2005 10:05:30 -0500
------=_Part_2359_18810799.1111331130747
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Hey all,
I followed Falk's recommendations. The code is shorter and actually
works for argument values of greater than two, but I don't really have
any way to time it. Please disregard my previous attempt.
Daniel
------=_Part_2359_18810799.1111331130747
Content-Type: text/plain; name="nsieve.cpp"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="nsieve.cpp"
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int nsieve(int);
int main(int argc, char *argv[])
{
int n =3D (argc =3D=3D 2) ? atoi(argv[1]) : 2;
int m;
=20
m =3D (1<<n)*10000;
cout << "Primes up to " << m << " " << nsieve(m) << endl;
=20
m =3D (1<<n-1)*10000;
cout << "Primes up to " << m << " " << nsieve(m) << endl;
=20
m =3D (1<<n-2)*10000;
cout << "Primes up to " << m << " " << nsieve(m) << endl;
=20
return EXIT_SUCCESS;
}
int nsieve(int m)
{
vector<bool> isPrime(m, true);
int count =3D 0;
=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_2359_18810799.1111331130747--