题目描述
素数判定,编写函数,参数是一个正整数n,如果它是素数,返回1,否则返回0。
输入格式
一行,一个整数 。
输出格式
如果是素数,输出 "yes",否则输出 "no";
样例
样例输入
25
样例输出
no
#include <iostream>
using namespace std;
int isPrime(int n)
{
if(n<2) return 0;
int i;
for(i=2;i*i<=n;i++)
if(n%i==0)
return 0;
return 1;
}
int main()
{
int n;
cin >> n;
if(isPrime(n)) cout << "yes";
else cout << "no";
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int isprime(int x)
{
if (x < 2)
return 0;
int n = static_cast<int>(sqrt(x));
for (int i = 2; i <= n; i++)
if (x % i == 0)
return 0;
return 1;
}
int main()
{
int x;
cin >> x;
cout << (isprime(x) ? "yes" : "no");
return 0;
}
```c++
#include <iostream>
#include <math.h>
using namespace std;
bool isz(int data)
{
if(data==0||data==1)
{
return false;
}
for (int i = 2; i <=sqrt(data); i++)
{
if(data%i==0)
{
return false;
}
}
return true;
}
int main()
{
int n;
cin>>n;
bool is=isz(n);
if(is)
{
cout<<"T"<<endl;
}
else
{
cout<<"F"<<endl;
}
return 0;
}
```