You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Function/素数.cpp

44 lines
604 B

3 years ago
#include <stdio.h>
#include <math.h>//使sqrt可以正常运行
3 years ago
int prime(int n)
{
int i ,m = 0;
if(n==0||n==1)//判断0和1的情况
3 years ago
return 0;
else
{
for (i = 2; i <= sqrt(n); i++)//减少循环次数,原理为一个数不可能有比平方根大的因子
3 years ago
{
if (n%i == 0)
{
m++;//m不为0时说明n除了1和它本身之外还有别的因子所以不是素数
3 years ago
}
}
if(m==0)
{
return 1;
}
else
{
return 0;
}
}
}
int main()
{
int num = 0;
scanf("%d", &num);
if (prime(num))//调用prime函数
3 years ago
{
printf("Yes");
}
else
printf("No");
return 0;
}