2012 素数判定

Problem Description

对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

 


Input

输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

 


Output

对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。


 


Sample Input

0 1
0 0


 


Sample Output

OK

 

 1 #include<cmath>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 bool prime(int n)
 6 {
 7     int i;
 8     if((n>2&&n%2==0))
 9         return 0;
10     for(i=3;i<=sqrt((double)n);i++)
11     {
12         if(n%i==0)
13             return 0;
14     }
15     return 1;
16 }
17 bool judge(int x, int y)
18 {
19     for(int i=x;i<=y;i++)
20     {
21         if(!prime(i*i+i+41))
22             return 0;
23     }
24     return 1;
25 }
26 
27 int main(){
28     int x,y;
29     while((cin >> x >> y)&&!(x==0&&y==0))
30     {
31         if(judge(x,y))
32             cout << "OK" << endl;
33         else
34             cout << "Sorry" << endl;
35     }
36     return 0;
37 }

 

posted on 2013-12-29 22:39  acm_shun  阅读(191)  评论(0)    收藏  举报

导航