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
#include <stdio.h>
#include <math.h>
int main()
{
int a, b;
while (scanf("%d %d", &a, &b) != EOF)//a,b分别指代x,y
{
if (a == 0 && b == 0)
break;
else
{
int flag = 0, c, d;
for (; a <= b; a++)
{
d = a * a + a + 41;
c = sqrt(d);
for (int i = 2; i <= c; i++)
if (d % i == 0)
flag++;
}
if (flag == 0)
printf("OK\n");
else
printf("Sorry\n");
}
}
return 0;
}
需注意输出格式,本题不需要考虑x,y的大小,题目中有提示,注意需要运算素数的是n^2+n+41而不是n