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
OKimport java.util.Scanner; public class POJ2012 { public static void main(String[] args) { Scanner cin = new Scanner(System.in); while (cin.hasNext()) { int x = cin.nextInt(); int y = cin.nextInt(); if (x == 0 && y == 0) break; int num = 0; for (int i = x; i <= y; i ++) { int n = i * i + i + 41; if (sushu(n)) num ++; } if (num == (y - x + 1)) System.out.println("OK"); else System.out.println("Sorry"); } } public static boolean sushu(int n) { if (n < 2) return false; if (n == 2) return true; for (int i = 3; i * i < n; i += 2) { if (n % i == 0) return false; } return true; } }