1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <iostream> #include "PrimeNumber.h" using namespace std;
void f1(int x) { bool judge; int k = 4; cout << "2 3 5 7 ";
for (int i = 11; i <= x; i = i + 2) { judge = true;
if ((i % 3 == 0) | (i % 5 == 0) | (i % 7 == 0)) continue;
for (int j = 11; j*j <= i; j = j + 2) { if (i%j == 0) { judge = false; break; } }
if (judge == true) { cout << i << " "; k = k + 1; if (k % 10 == 0) cout << "\n"; } }
cout << endl << endl << "共" << k << "个素数"; }
void f2(int x) { int *a = new int[x - 1]; int i, j;
for (i = 0; i < x - 1; ++i) a[i] = i + 2;
for (j = 0; j < x - 1; ++j) { if (a[j] != 1) { for (i = j + 1; i < x - 1; ++i) { if (a[i] % a[j] == 0) a[i] = 1; } } }
for (i = 0, j = 0; i < x - 1; ++i) { if (a[i] != 1) { cout << a[i] << " "; j = j + 1; if (j % 10 == 0) cout << "\n"; } }
cout << endl << endl << "共" << j << "个素数"; }
|