fork download
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. //http://p...content-available-to-author-only...j.com/problems/ZABAWA/
  7.  
  8. public class Main {
  9.  
  10. public static void main(String[] args) throws IOException {
  11. new Main().zadanie();
  12. }
  13.  
  14. private void zadanie() throws IOException {
  15. String s;
  16. String[] tab;
  17. int x, y;
  18.  
  19. while ((s = in.readLine()) != null) {
  20. tab = s.split(" ");
  21. x = Integer.parseInt(tab[0]);
  22. y = Integer.parseInt(tab[1]);
  23. if (isPrime(x)) {
  24. y++;
  25. while (true) {
  26. if (isPalidrome(y)) {
  27. System.out.println(y);
  28. break;
  29. } else {
  30. y++;
  31. }
  32. }
  33. } else {
  34. y++;
  35. while (true) {
  36. if (isPalidrome(y)) {
  37. System.out.println(y);
  38. break;
  39. } else {
  40. y--;
  41. }
  42. }
  43. }
  44. }
  45. }
  46.  
  47. private boolean isPrime(int n) {
  48. for (int i = 2; i < Math.sqrt(n)+1; i++) {
  49. if (n % i == 0) {
  50. return false;
  51. }
  52. }
  53. return true;
  54. }
  55.  
  56. private boolean isPalidrome(int n) {
  57. String s = n + "";
  58. return new StringBuilder(s).reverse().toString().equals(s) ? true : false;
  59. }
  60. }
  61.  
Success #stdin #stdout 0.04s 712192KB
stdin
2 8998
10 8998
stdout
8998
8998