//埃氏筛法 //C++版代码 #include <iostream> #include <vector> using namespace std; int main() { vector<bool> isPrime(1001, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= 1000; i++) { if (isPrime[i]) { for (int j = i; j * i <= 1000; j++) isPrime[i * j] = false; } } int n; while (cin >> n) { if (isPrime[n]) cout << "yes" << endl; else cout << "no" << endl; } return 0; } //Java版代码 import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean[] isPrimes = new boolean[10001]; Arrays.fill(isPrimes, 2, 10001, true); for (int i = 2; i * i <= 10000; i++) { if (isPrimes[i]) { for (int j = i; j * i <= 10000; j++) isPrimes[j * i] = false; } } while (sc.hasNextInt()) { int n = sc.nextInt(); if (isPrimes[n]) System.out.println("yes"); else System.out.println("no"); } } } //欧拉筛法 //C++版代码 #include <iostream> #include <vector> using namespace std; int main() { vector<int> primes; bool isPrime[10001]; for (int i = 2; i <= 10000; i++) isPrime[i] = true; for (int i = 2; i <= 10000; i++) { if (isPrime[i]) primes.push_back(i); for (int j = 0; j < primes.size() && i * primes[j] <= 10000; j++) { isPrime[i * primes[j]] = false; if (i % primes[j] == 0) break; } } int n; while (cin >> n) { if (isPrime[n]) cout << "yes" << endl; else cout << "no" << endl; } return 0; } //Java版代码 import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Integer> primes = new ArrayList<>(); boolean[] isPrimes = new boolean[10001]; Arrays.fill(isPrimes, 2, 10001, true); for (int i = 2; i <= 10000; i++) { if (isPrimes[i]) primes.add(i); for (int j = 0; j < primes.size() && i * primes.get(j) <= 10000; j++) { isPrimes[i * primes.get(j)] = false; if (i % primes.get(j) == 0) break; } } while (sc.hasNextInt()) { int n = sc.nextInt(); if (isPrimes[n]) System.out.println("yes"); else System.out.println("no"); } } }