题解 | 素数判断
素数判断
https://www.nowcoder.com/practice/5ab1b9690af047699e96c87dee65def4
#include <iostream> #include <cmath> using namespace std; bool isPrime(int n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; int sqrt_n = sqrt(n); for (int i = 3; i < sqrt_n; i += 2) { if (n % i == 0) return false; } return true; } int main() { int t, a; cin >> t; while (t--) { cin >> a; if (isPrime(a)) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }