判断是否是素数

来源:互联网 发布:linux cpu使用率过高 编辑:程序博客网 时间:2024/04/30 12:02
#include<iostream>#include<cmath>using namespace std;bool  judge(int test){   if(test <= 0 )      return 0;   double  sqrtm = sqrt(test * 1.0);   int i=1;   while(i<sqrtm)   {      if(test%2 != 0 )      {         i+=2;      }      if(test %i ==0)      {         return 0;      }   }   return 1;}int main(){   int test;   cin>>test;   if(judge(test))   {      cout<<test<<"is a prime"<<endl;   }   else cout<<test<<"is not a prime"<<endl;   getchar();   getchar();}


反正不能被2整除就不能被4 6 8 整除...