码迷,mamicode.com
首页 > 其他好文 > 详细

杭电2012-素数判定

时间:2015-03-08 22:56:30      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

#include <cstdlib>
#include <iostream>
#include<cmath>
using namespace std;

//高效判定素数
bool prime(int n)
{
     if(n==2)
        return true;
     if(n%2==0)
        return false;
     int sqrtn=(int)sqrt((double)n);
     bool flag=true;
     for(int i=3;i<=sqrtn;i+=2)
        if(n%i==0)
         flag=false;
     return flag;
}
int main(int argc, char *argv[])
{
    int x,y,count;
    while(cin>>x>>y)
    {
       if(x==0&&y==0)
        break;
        count=0;
        for(int i=x;i<=y;i++)
        {
          if(!prime(i*i+i+41))
           count++;
        }
        if(count==0)
          cout<<"OK"<<endl;
        else
          cout<<"Sorry"<<endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

杭电2012-素数判定

标签:

原文地址:http://www.cnblogs.com/wft1990/p/4322432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!