Here is an updated version, using the square root. Wow, it only takes 8.59 seconds (as opposed to 14.4 without) to find all primes below 100,000,000. Thanks for the help.
Code:
#include <iostream>
#include <time.h>
#include <stdio.h>
const unsigned int max = 100000000;
int
main (int argc, char *argv[])
{
float t, c;
bool *prime = new bool[max];
for (unsigned int ctest = 0; ctest < max; ctest++)
{
prime[ctest] = 1;
}
c = clock ();
for (unsigned int ctest = 2; ctest < sqrt(max); ctest++)
{
if (prime[ctest] == 1)
{
for (int citer = ctest * 2; citer < max; citer += ctest)
{
prime[citer] = 0;
}
}
}
int pnum = 0;
for (unsigned int ctest = 0; ctest < max; ctest++)
{
if (prime[ctest] == 1)
{
pnum++;
}
}
t = clock () - c;
t = t / (float) CLOCKS_PER_SEC;
std::cout << "It took " << t << "seconds" << std::endl;
std::cout << "There are: " << pnum << " primes below " << max << std::endl;
}