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”,每組輸出占一行。
#include <cstdio>
bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
return false;
}
return true;
}
int main()
{
int x, y;
while (scanf("%d %d", &x, &y))
{
if (x == 0 && y == 0)break;
bool q = true;
for (int i = x + 1; i < y; i++)
{
if (!isprime(i * i + i + 41))
{
q = false;
break;
}
}
if (q)
printf("OK\n");
else
printf("Sorry\n");
}
return 0;
}