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 <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
#define f(n) n*n+n+41
using namespace std;
ll sushu(ll a)
{
if(a==2) return 1;
if(a<=1||a%2==0) return 0;
ll j=3;
while(j<=(ll)sqrt(a))
{
if(a%j==0)
return 0;
j+=2;
}
return 1;
}
int main()
{
ll x, y;
while(scanf("%lld%lld", &x, &y)!=EOF)
{
if(x==0&&y==0) break;
ll i;
for(i=x; i<=y;i++)
{
if(sushu(f(i))==0) break;
}
if(i>y) printf("OK\n");
else printf("Sorry\n");
}
return 0;
}
不可以直接跳出循环。思路很重要。