各位請看一下:
#include <iostream>
using namespace std;
int main(){
int a[168] = {2, 3}, i, n, j;
for (i = 2, n = 6; n <= 1000; n += 6){
for (j = 0; j < i; j++) if (!((n-1) % a[j])) break;
if (j == i) a[i++] = n - 1;
for (j = 0; j < i; j++) if (!((n+1) % a[j])) break;
if (j == i) a[i++] = n + 1;
}
while (cin >> n, n)
if (n == 2 || n == 3) cout << 0 << endl;
else if (n%6 == 1 && n != 1 || n%6 == 5){
for (i = 0; a[i] * a[i] <= n; i++)
if (!(n % a[i])) {cout << 1 << endl; break;}
if (a[i] * a[i] > n) cout << 0 << endl;
}
else cout << 1 << endl;
}
各位請看一下:
#include
using namespace std;
int main(){
int a[168] = {2, 3}, i, n, j;
for (i = 2, n = 6; n <= 1000; n += 6){
for (j = 0; j < i; j++) if (!((n-1) % a[j])) break;
if (j == i) a[i++] = n - 1;
for (j = 0; j < i; j++) if (!((n+1) % a[j])) break;
if (j == i) a[i++] = n + 1;
}
while (cin >> n, n)
if (n == 2 || n == 3) cout << 0 << endl;
else if (n%6 == 1 && n != 1 || n%6 == 5){
for (i = 0; a[i] * a[i] <= n; i++)
if (!(n % a[i])) {cout << 1 << endl; break;}
if (a[i] * a[i] > n) cout << 0 << endl;
}
else cout << 1 << endl;
}