#include #include #include using namespace std; /* * Find the first n primes by simple elimination. */ /* See if it has a divisor. Assumes num is odd, since we just skip those in the main algorithm. So we assume 2 is not a factor. */ bool has_divisor(int num) { int max = num / 3; for(int i = 3; i <= max; ++i) if(num % i == 0) return true; return false; } /* Output the next number. Arranges them in 10 per row. */ void output(int n) { static const int LINE_SIZE = 12; static int linecount = 0; if(linecount >= LINE_SIZE) { cout << endl; linecount = 0; } else if(linecount > 0) cout << " "; cout << setw(5) << n; ++linecount; } int main(int argc, char **argv) { int count = 200; if(argc > 1) count = atoi(argv[1]); output(2); --count; for(int n = 3; true; n += 2) { if(!has_divisor(n)) { output(n); if(--count <= 0) break; } } cout << endl; }