0

Dear Friends:

  • As much as strings, some numbers are also palindrome. For instance: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ... , 101, 111, ... ,753537, ... and so on.

  • Here is the thing, We need to figure a way to find first 10.000 palindromic numbers in order to respond user's entry. Starts from 1 to 10000th palindromic number. For example if user enters 12 it means what is the 12th palindromic number between 1 and 10.000 ?

  • The input consists of a series of lines with each line containing one integer value i (1 <= i <= 10000). This integer value i indicates the index of the palindrome number that is to be written to the output, where index 1 stands for the first palindrome number (1), index 2 stands for the second palindrome number (2) and so on.

EX:

Input 1 --> Output should be: 1

Input 12 --> Output should be: 33

Input 24 --> Output should be: 151

    import java.util.Scanner;

    public class xth_palindrome
    {
        // Some Code may be here

        public static void main(String[] args)
        {
            @SuppressWarnings("resource")
            Scanner read = new Scanner(System.in);
            System.out.println("Enter values as much as you want. To stop Enter \"0\" ");

            int Xth;

            do
            {
                Xth = read.nextInt();

                // Some coding here 

                System.out.println(Xth + " palindromic num is " + "????");

            } while(Xth != 0);
        }
    }
  • By the way: time limit is 1 second. Considering these factors What is the right Algorithm to solve this problem ? If you could help me and show the solution code wise in Java I would very appreciate it. Thanks for checking!
Jon Snow
  • 35
  • 5
  • What do you mean you already know the first 10000 palindromic numbers? Would it not then be as simple as putting them into an array and checking `palindromeArray[Xth+1]`? – RogueBaneling Dec 03 '14 at 18:17
  • "Let's assume that we know the first 10,000..." "databases are not allowed." What do you mean by "database"? How can your program "know" 10,000 facts if they're not in a database? If your program has to compute the first 10,000 palindromic numbers then it doesn't _know_ them: It knows how to _find_ them. – Solomon Slow Dec 03 '14 at 18:18
  • It is not evident what you need help with. Please post code of what you have already tried. – C0D3LIC1OU5 Dec 03 '14 at 18:21
  • 2
    I think you could solve this by thinking about it this way: All 1-digit numbers are palindromes. What 2-digit numbers are palindromes, how many are there, and if we were asked to find the Nth 2-digit palindrome, how would we do that? Then do the same for 3-digit palindromes, 4-digit palindromes, and so on. If you can compute how many of each there are, then you can say "if X is in 1-9 it must be one digit, if it's in 10 to (9+N) it must be 2 digits, if it's in 10+(10+N) to something, it must be 3 digits... – ajb Dec 03 '14 at 18:21
  • Sorry, I fixed the ambiguity. I meant we need to find a way. – Jon Snow Dec 03 '14 at 18:29
  • I added some Sample Input and Output. You may also want to check. – Jon Snow Dec 03 '14 at 18:37
  • An internet search engine reveals that this has been extensively studied and some results are [here](http://johanjeuring.blogspot.fi/2007/08/finding-palindromes.html). –  Dec 03 '14 at 19:53
  • FYI: I just noticed that the number 753537 that you posted in your question is not correct since it's not a palindrome (perhaps you meant 753357?). – ajb Dec 03 '14 at 20:10

2 Answers2

0

We can iterate through the palindromes quite quickly. Note that

  1. If there is an odd palindrome ABCBA, the next larger palindrome will be ABDBA where D=C+1

  2. If there is an even palindrome ABCCBA, the next larger palindrome will be ABDDBA where D=C+1

The reasoning is simple. Any other number will also increment a larger MSB and hence the next higher palindrome will have changes in the centre.

Now if C = 9, we will need to increment B and reset C to 0 making the cases become AE0EA and AE00EA where E=B+1. This method is easily extensible and you can iterate through palindromes. Since we need to find at most 10,000 of them, a second should be more than enough for an iterative approach.

user1952500
  • 6,611
  • 3
  • 24
  • 37
0

Maybe not "the very best way", but works fine.

And it does the job in less than 1 sec (depending of your hardware).

I've tested here.

import java.util.Scanner;

public class HelloWorld{

     public static void main(String []args){

            Scanner read = new Scanner(System.in);
            System.out.println("Enter values as much as you want (lower than 100000).\n To stop Enter \"0\" ");

            int Xth;

            do
            {
                Xth = read.nextInt();


                // Some coding here 
                if (Xth > 100000)
                {
                    System.out.println("Type a number lower than 100000");
                    continue;
                }
                int count = 0;
                for (int i = 1; i <= 1000000000; i++)
                {
                    if (!isPalindrome(i))
                        continue;

                    count++;
                    if (count == Xth)
                    {
                        System.out.println(Xth + "th palindromic number is " + i);
                        break;
                    }
                }
                if (count != Xth)
                    System.out.println("I can't compute that!");


            } while(Xth != 0);
     }

     private static StringBuilder sb = new StringBuilder();

     private static boolean isPalindrome(int i)
     {
        sb.setLength(0);
        sb.append(i);
        return  sb.toString().equals(sb.reverse().toString());
     }
Christian
  • 7,062
  • 9
  • 53
  • 79
  • It is going to be a very late respond but first of all thank you very much for your answer it works perfect. Only one problem, in the worst-case scenario(When user enters ten thousand) it takes slightly more than 1 second to compute. If you have any other idea to improve this solution I would like to hear that. Thanks again! – Jon Snow Apr 17 '15 at 23:23