Results 1 to 5 of 5
  1. #1
    Ben Bublil is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Question Creating Palindromes Using Recursion

    Hello, I need help.
    I just started studying recursion and I wanted to know how to create a palindrome number going up from 1 to n then back to 1 like this: "12345...n...54321".
    I've done one going downwards and then upwards like this: "n...4321234...n".

    Here's my code:

    Java Code:
    import java.util.*;
    public class PalindromeTest
    {
      public static void downPalindrome(int n)
      {
        if(n>0)
        {
          System.out.print(n);
          downPalindrome(n-1);
          if(n!=1)
            System.out.print(n);
        }
      }
      public static void main(String args[])
      {
        Scanner in = new Scanner (System.in);
        int x=in.nextInt();
        downPalindrome(x);
      }
    }
    If my input is 5, the output would be: "543212345".
    Can anyone help?

    Thanks a lot!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Creating Palindromes Using Recursion

    Let's do the first type of palindromes first (the other one is similar): assume a current number i and a maximum number n; if i == n, the inner part of the palindrome is just n; otherwise, the inner part of the palindome is P(i+1, n) surrounded by i, both on the left and the right part, i.e. P(i, n) == if (i == n) then n else i P(i+1, n) i. An example: n == 3, so P(1, 3) == 1 P(2, 3) 1 == 1 2 P(3, 3) 2 1 == 1 2 3 2 1. It doesn't take much to turn this to working Java code.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Ben Bublil is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Creating Palindromes Using Recursion

    Thanks! Although I was trying to create it using only 1 perimeter.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Creating Palindromes Using Recursion

    Quote Originally Posted by Ben Bublil View Post
    Thanks! Although I was trying to create it using only 1 perimeter.
    But then you have to create two methods: one for the left part and one for the right part of the palindrome and both can be recursive. You can hide one parameter from the world by doing this:
    P(n) == P(1,n), where the second method does all the work and the first method is just a convenience method for the user.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Creating Palindromes Using Recursion

    p.s. you can do the second type of palindrome with a one parameter method. In general: if the sentinel value (either 0 or n) can be considered some constant value, both palindrome types can be done with a one parameter method.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  2. Problem with Palindromes
    By thaotic in forum New To Java
    Replies: 5
    Last Post: 03-14-2012, 12:01 PM
  3. Counting number of palindromes in a file
    By vuzuggu in forum New To Java
    Replies: 3
    Last Post: 10-20-2011, 05:52 PM
  4. Project Euler #4 - Palindromes
    By Awk34 in forum New To Java
    Replies: 12
    Last Post: 12-19-2010, 10:28 AM
  5. Find All Palindromes
    By spleenlol in forum New To Java
    Replies: 6
    Last Post: 02-04-2010, 02:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •