算法: 让m被2到sqrt(m)除,为真则提前结束,此时i必然小于等于sqrt(m),m这时不是素数;为假则i必然大于等于sqrt(m),m这时是素数。
#include
#include
main()
{
int i,k,m;
scanf("%d",&m);
k=sqrt(m);
for (i=2;i<=k;i++)
if (m%i==0) break;
if (i>=k+1) printf("%d is a prime number\n",m);
else printf("%d is not a prime number\n",m);
}
阅读(3124) | 评论(5) | 转发(0) |