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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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