判断素数(天梯)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/beijiafei/article/details/80169282

基本判断思路
在一般领域,对正整数n,如果用2到 sqrt(n) 之间的所有整数去除,均无法整除,则n为质数。
质数大于等于2 ,不能被它本身和1以外的数整除

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    int N;
    long long x;
    cin>>N; 
    int i=1;
    while(i<=N){
        cin>>x;
        bool check = true;
        for(int j=2;j<=(int)sqrt(x);j++){
            if(x % j == 0){
                check = false;
                break;
            }
        }
        if(x==1){
            check = false;
        }
        if(check){
            cout<<"Yes"<<endl;
        }
        else{
            cout<<"No"<<endl;
        }
        i++;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/beijiafei/article/details/80169282