Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class PalindromicPolite{
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         int input = sc.nextInt();
  7.         System.out.println(counter(input));
  8.        
  9.     }
  10.  
  11.     public static boolean isPalindromic(int x){
  12.         String s = x + "";
  13.         for (int i = 0; i<s.length(); i++) {
  14.             if(s.charAt(i) != s.charAt(s.length()-i-1)){
  15.                 return false;
  16.             }
  17.         }
  18.         return true;
  19.     }
  20.  
  21.     public static boolean isPolite(int x){
  22.         for (int i = 1; i < x/2+1; i++) {
  23.             int guess = i;
  24.             int nextNumber = i+1;
  25.             while (guess < x) {
  26.                 guess += nextNumber;
  27.                 if (guess == x) {
  28.                     return true;
  29.                 }
  30.                 nextNumber++;
  31.             }
  32.         }
  33.         return false;
  34.     }
  35.     public static int counter(int x){
  36.         int count = 0;
  37.         outerloop:
  38.         for (int i = 0; true; i++) {
  39.             count++;
  40.             if (isPolite(i) == true && isPalindromic(i) == true && count == x) {
  41.                 break outerloop;
  42.             }
  43.         }
  44.         return count;
  45.     }
  46.    
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement