[Shootout-list] nsieve g++ c++
Daniel Skiles
Daniel Skiles <dskiles@gmail.com>
Sun, 20 Mar 2005 09:57:08 -0500
------=_Part_2355_27533330.1111330628675
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
> The elements initially contain random stuff, not all "0" (or all
> "false" in this case). I'm not sure that this is actually a problem
> for your program, though, maybe you initialize all explicitely
> somewhere.
I initialize all of the values to boolean true in the nsieve function.
I've attached a copy of a version with a proper delete[] to this
message.
> I'd just use vector<bool> and avoid this completely :-)
That's a really good idea. I'll start working on that.
Thanks,
Daniel
------=_Part_2355_27533330.1111330628675
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_2355_27533330.1111330628675--