由于水平原因,博客大部分内容摘抄于网络,如有错误或者侵权请指出,本人将尽快修改

4.1数学知识-素数

1.试除法求素数

质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。

#include <iostream>
#include <math.h>
using namespace std;
bool is_prime(int x)
{
    if (x < 2) return false;
    for (int i = 2; i <=sqrt(x); i ++ )
        if (x % i == 0)
            return false;
    return true;
}
int main(){
    int x;
    cin>>x;
    if(is_prime(x)){
        cout<<"true";
    }else{
        cout<<"false";
    }
    return 0;
}

 

posted @ 2020-11-14 16:52  小纸条  阅读(240)  评论(0)    收藏  举报