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;
}
加油啦!加油鸭,冲鸭!!!

浙公网安备 33010602011771号