The Artima Developer Community
Sponsored Link

Java Answers Forum
Palindroms smordnilaP

13 replies on 1 page. Most recent reply: Mar 22, 2007 8:32 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
allant gomez

Posts: 8
Nickname: allant
Registered: Mar, 2007

Palindroms smordnilaP Posted: Mar 7, 2007 5:00 AM
Reply to this message Reply
Advertisement
Hello People!
I have an interesting problem for you to solve, since I dont understand it. If anyone manages to do it please send me the code at allan_g89@yahoo.com

----------------------Palindroms smordnilaP----------

The following problem deals with Palindroms composed of digits. A number is a palindrom, if the sequence of signs (digits or characters) read from left to right and read from right to left are identical. Now, given the number 65 with base 10, adding the number read from right to left , that means 56, leads to 121. By definition 121 is a palindrom. With another number you might have to repeat this step until the sum is of the required palindrom form. eg. 87:
87 + 78 = 165
165 + 561 = 726
726 + 627 = 1353
1353 + 3531 = 4884
The number of steps is 4.
This works in any base with any number. Naturally the number of steps increases incredibly fast, so there exist numbers in base 10 that requires more than 10,000 steps. You will have to find the numbers of steps of a given number in all the bases 15 down to 2. When a Number is in an illegal form in a base, the number of Steps will be represented by a ``?".

----------Example----------

Base 15 87 + 78 = 110
110 + 011 = 121 2 steps
Base 14 87 + 78 = 111 1 step
Base 13 87 + 78 = 132
132 + 231 = 363 2 steps
Base 12 87 + 78 = 143
143 + 341 = 484 2 steps
Base 11 87 + 78 = 154
154 + 451 = 5A5 2 steps
Base 10 87 + 78 = 165
165 + 561 = 726
726 + 627 = 1353
1353 + 3531 = 4884 4 steps
Base 9 87 + 78 = 176
176 + 671 = 857
857 + 758 = 1726
1762 + 2671 = 7543
7543 + 3457 = 12111
12111 + 11121 = 23232 6 steps
Base 8 illegal ? steps
Base 7 illegal ? steps
Base 6 illegal ? steps
Base 5 illegal ? steps
Base 4 illegal ? steps
Base 3 illegal ? steps
Base 2 illegal ? steps

---------Input and Output---------------
The input contains several lines, each of them having a legal base 15 integer.

For each line of the input print a single line containing the 14 number of steps in all bases 15 down to 2 separated by a blank space. The number of steps will never be bigger than 100.
-----------Sample Input----------------
87
ED
-----------Sample Output---------------
2 1 2 2 2 4 6 ? ? ? ? ? ? ?
19 ? ? ? ? ? ? ? ? ? ? ? ? ?

Pls!!!


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Palindroms smordnilaP Posted: Mar 7, 2007 5:52 AM
Reply to this message Reply
Have you considered:
a) Opening a Java book and learning something, or
b) Talking to your tutor about what it is you don't understand?

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Palindroms smordnilaP Posted: Mar 7, 2007 5:57 AM
Reply to this message Reply
Have you considered:
a) Opening a Java book and learning something, or
b) Talking to your tutor about what it is you don't understand?

allant gomez

Posts: 8
Nickname: allant
Registered: Mar, 2007

Re: Palindroms smordnilaP Posted: Mar 7, 2007 5:57 AM
Reply to this message Reply
I already spoke to my tutor but he doesnt help me. and I know how to program in java. But the program is too difficult for me.

Darren Tarbard

Posts: 2
Nickname: tabs
Registered: Aug, 2006

Re: Palindroms smordnilaP Posted: Mar 7, 2007 7:59 AM
Reply to this message Reply
Post the code you have so far, if you know how to program in java presumably you have some code to show?

So far it looks like you have just posted your homework and added pls!!!!

allant gomez

Posts: 8
Nickname: allant
Registered: Mar, 2007

Re: Palindroms smordnilaP Posted: Mar 9, 2007 5:42 AM
Reply to this message Reply
import java.io.*;

public class Palindroms {

public static void main(String[] args) throws IOException {
int num,num1;

BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));

Base Base= new Base();

System.out.print("Ingrese numero Palindromo : ");
num = Integer.parseInt(in.readLine( ));
num1= Base.palindromo(num);
System.out.println(num + "+" + num1 + "=");

}
}

public class Base {


public int palindromo(int num){
int num_inv = 0;
int div_entera = num;
int resto_div = 0;
while (div_entera != 0)
{
resto_div = div_entera % 10;
div_entera = div_entera / 10;

num_inv = num_inv * 10 + resto_div;
}

//System.out.println("El numero " + num + " invertido es " + num_inv);
return num_inv;
}


public void Base_15(int num) {
//no idea :P
}

allant gomez

Posts: 8
Nickname: allant
Registered: Mar, 2007

Re: Palindroms smordnilaP Posted: Mar 9, 2007 6:59 AM
Reply to this message Reply
theres what I already done but I dont now how to sum in base 15?????

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Palindroms smordnilaP Posted: Mar 12, 2007 8:53 AM
Reply to this message Reply
Why don'T you just copy everything from the class "Base" and replace every "10" with "15"?

Or even better: Replace it with a variable.
So you can use the class for different bases.

allant gomez

Posts: 8
Nickname: allant
Registered: Mar, 2007

Re: Palindroms smordnilaP Posted: Mar 14, 2007 8:52 AM
Reply to this message Reply
public void Base_9(int num, int num1) {
int div_entera = num, div_compl = num1;
int resto_div = 0, rest_div = 0;
int sum = 0, lleva = 0,count= 0;
String str="",cad="";
int result=0;
System.out.print("Base 9 ");
while (num1 != result) {

while (div_entera != 0)
{
resto_div = div_entera % 10;
div_entera = div_entera / 10;

rest_div = div_compl % 10;
div_compl = div_compl / 10;

sum = resto_div + rest_div;
if (sum>8) sum += 1;

if (lleva == 1) {
sum +=1;
lleva = 0;
}

if (div_entera != 0) {
if (sum >= 10) {
lleva = 1;
sum = sum %10;
}
}
str=String.valueOf(sum);
cad = str + cad;

}
count += 1;
result=Integer.valueOf(cad).intValue();

System.out.println(num + "+" + num1 + "=" + result);
num1 = palindromo(result);
if (num1 != result) num = result;
div_compl = num1; div_entera = num; str=""; cad="";
}
System.out.println(count + " Steps");
}
Already done the base 9 :) is you have any commnets pls tall me

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Palindroms smordnilaP Posted: Mar 16, 2007 8:54 AM
Reply to this message Reply
Too complicated. Make Java do the work for you. Also, you can't easily do base(n) arithmatic with ints because they're always base(10). Use the Integer class instead.

To add two numbers in any base and get the result in that base:
public String sum(String num1, String num2, int base)
    {
        // Convert both numbers to base 10 and add them.
        int num1base10 = Integer.parseInt(num1, base);
        int num2base10 = Integer.parseInt(num2, base);
        int sumInBase10 = num1base10 + num2base10;
        // Return the result in the given base.
        return Integer.toString(sumInBase10, base);
    }
To get the palindrome of a String:
public String getPalindrome(String s)
    {
        return (new StringBuffer(s)).reverse().toString();
    }
These can then be used in a loop that simulates what you were doing before:
public void Base_N(String num, String num1, int base) {
        int count= 0;
        System.out.println("Base " + base);
        String sNum  = num;
        String sNum1 = num1;
        while (true) 
        {
            count++;
            String result = sum(sNum, sNum1, base);
            System.out.println(num + "+" + num1 + "=" + result);
            
            if (result.equals(getPalindrome(result)))
                break;
            
            sNum = result;
            sNum1 = getPalindrome(result);
        }
        System.out.println(count + " Steps");
    }
and call it as follows:
Base b = new Base();
        b.Base_N("87", "78", 9);
        b.Base_N("87", "78", 11);
        b.Base_N("abc0", "abc1", 14);
It's very rough code and can be broken easily (because I didn't do any test-first coding) but I think it does approximately what you want.

allant gomez

Posts: 8
Nickname: allant
Registered: Mar, 2007

Re: Palindroms smordnilaP Posted: Mar 22, 2007 2:03 AM
Reply to this message Reply
omg man a 1000 of thx!! Thank you !! u just make 1 mistake :P

System.out.println(num + "+" + num1 + "=" + result);

allant gomez

Posts: 8
Nickname: allant
Registered: Mar, 2007

Re: Palindroms smordnilaP Posted: Mar 22, 2007 2:05 AM
Reply to this message Reply
 
System.out.println(sNum + "+" + sNum1 + "=" + result);

allant gomez

Posts: 8
Nickname: allant
Registered: Mar, 2007

Re: Palindroms smordnilaP Posted: Mar 22, 2007 2:10 AM
Reply to this message Reply
public String getPalindrome(String s)
    {
        return (new StringBuffer(s)).reverse().toString();
    }

want to know something. what StringBuffer do? or xplain me this -new StringBuffer(s)).reverse().toString();

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Palindroms smordnilaP Posted: Mar 22, 2007 8:32 AM
Reply to this message Reply
String is an immutable, read-only class. Immutable means that the value of a String cannot be changed, instead - if you try to change a String - the old String is discarded and a new String object is created that contains the results.

Strings are very useful but not very flexible. To manipulate Strings, Java provides the StringBuffer class. One of the useful methods just happens to be reverse(). You'll find better explanations by entering "String StringBuffer" (without the quotes) into Google.

The line
return (new StringBuffer(s)).reverse().toString()
could equally be written as
StringBuffer sb = new StringBuffer(s);
sb.reverse();
s = sb.toString();
return s;

Flat View: This topic has 13 replies on 1 page
Topic: Palindroms  smordnilaP Previous Topic   Next Topic Topic: Best programming language to build to product prototype (advice)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use