I am having some problem in compiling the code for the following problem....can anybody provide an alternative code for it...
Problem:- A palindrome is a number such that when all its digits are reversed it is still the same as the original number. For example, the number 7007 is a palindrome. Reversing all its digits will give 7007 which is the same as the original number. Also, 997 is not a palindrome as reversing its digits gives 799 which is not the same as 997.
This problem is to do with palindromes but with a small twist. Find all the 3 and 4-digit positive integers that do not become a palindrome even after the following steps are repeated up to a 10,000 times. To understand the following steps better let us take a 4-digit positive integer, 4824 as an example.
1. Reverse the original number. Reversing 4824 yields 4284. 2. Add the original number and the reversed number. Adding 4824 to 4284 yields 9108. 3. Check if the new number is a palindrome. The reverse of 9108 is 8019 which is not the same as 9108. Hence 9108 is not a palindrome. 4. If the new number is not a palindrome replace the original number with the new number and repeat steps 1 through 4. In this case we replace 4824 with 9108 and repeat the steps all over again.
One iteration is counted as a single pass through all the four above steps.
Consider another example with a four digit positive integer 5689.
As per the above algorithm 5689 becomes a Palindrome in 4 iterations. The number 9999 for example does not throw up a palindrome even after 10 iterations.
Hello...well I have been able to solve the 'out of memory' error wth the following code but now the problem is that I am unable to send inputs to the BigInteger...I dont knw how to give inputs to the BigInteger....I have to check for all 3 and 4 digit numbers...also I have to count the number of iterations after which a number becomes a palindrome...
The Code is:- import java.math.BigInteger;
public class Palindrome1 {
public static boolean verbose = true;
public static void main(String[] args) { BigInteger l = new BigInteger("9867"); try { if (l.compareTo(BigInteger.ZERO) < 0) { System.err.println(l+ " -> TOO SMALL"); //continue; } System.out.println(l+ "->" + findPalindrome(l)); } catch (NumberFormatException e) { System.err.println(l+ "-> INVALID"); } catch (IllegalStateException e) { System.err.println(l+ "-> " + e); }
}
public static BigInteger findPalindrome(BigInteger num) {
if (num.compareTo(BigInteger.ZERO) < 0) throw new IllegalStateException("negative"); if (isPalindrome(num)) return num; return findPalindrome(num.add(reverseNumber(num))); }
/** A large number */ protected static final int MAX_DIGITS = 1000;
/** Check if a number is palindromic. */ public static boolean isPalindrome(BigInteger num) { String digits = num.toString(); int numDigits = digits.length(); if (numDigits >= MAX_DIGITS) { throw new IllegalStateException("too big,can't be a palindrome"); } // Consider any single digit to be as palindromic as can be if (numDigits == 1) return true; for (int i=0; i<numDigits/2; i++) { if (digits.charAt(i) != digits.charAt(numDigits - i - 1)) return false; } return true; }
static BigInteger reverseNumber(BigInteger num) { String digits = num.toString(); int numDigits = digits.length(); char[] sb = new char[numDigits]; for (int i=0; i<digits.length(); i++) { sb = digits.charAt(numDigits - i - 1); } return new BigInteger(new String(sb)); } }
Please suggest proper changes or an alternative way to approach the problem...
Fist of all, please use the java tags if you post code. 1. the code is easier to read then 2. you won't get the problem, that if you use i or b as an index it will be kicked out of the code and changes only the apperance of you code.
In my opinion there is a more elegant way to invert the digits in an integer number:
privatestaticint invertDigits(long number) {
long newNumber = 0;
//You can here do something to make sure 'number' is not negative
while (number >= 0) {
newNumber = newNumber * 10 + number %10;
number = number / 10;
}
return newNumber;
}
Now you just have to compare the numbers and in case they don't fit call your isPalindrome method again with the sum of the numbers.
Well, you made the same error reversing the number as I did, you just made it worse. int is even less useful then long.
1. This palindrome operation can generate VERY large numbers. If long is not able to store them, int certainly isn't.
2. They want to o this on a text base. Meaning: reverse("0001234") == "4321000" Your (and my) method would generate this: reverse(0001234) == 4321
The recursion of the find palindrome method is rather easy.
I think the idea behind our versions is the same. Here's the code I came up with doing this during break time:
publicclass PalindromeFinder {
/** inverts the decimal digits of a long value. myNumber must be bigger or equal 0
* @param myNumer The number to be inverted
* @return A number with the digits in reverse order
*/
privatestaticlong invertDigits(long myNumber) {
long newNumber = 0;
while (myNumber > 0) {
newNumber = newNumber * 10 + myNumber % 10;
myNumber = myNumber / 10;
}
return newNumber;
}
/** Searches for a palindrome. Increments myNumber by invertDigits(myNumber) if myNumber is not a palindrome until the resulting value is a palindrome
* @param myNumber The value for whom a palindrome is to be found. if (myNumber < 0) the result will be -searchForPalindrome(-myNumber)
* @return The palindrome of the number.
*/
publicstaticlong searchForPalindrome(long myNumber) {
if (myNumber < 0)
return -searchForPalindrome(-myNumber);
long invertedNumber = 0;
int counter = 0;
do {
myNumber+=invertedNumber;
invertedNumber = invertDigits(myNumber);
counter ++;
} while (invertedNumber != myNumber);
System.out.print(counter);
return myNumber;
}
/** inverts the decimal digits of a long value. myNumber must be bigger or equal 0
* @param myNumer The number to be inverted
* @param divisor must in [2, 10]
* @return A number with the digits in reverse order
*/
privatestaticlong invertDigits(long myNumber, int divisor) {
long newNumber = 0;
while (myNumber > 0) {
newNumber = newNumber * divisor + myNumber % divisor;
myNumber = myNumber / divisor;
}
return newNumber;
}
publicstaticvoid main(String[] args) {
// System.out.println(invertDigits(415,8));
long[] myNumbers = newlong[]{1495, 919, 1234, 415, 71247, 2572, 7257, -8316717, 512479};
for (long myNumber : myNumbers) {
System.out.print("Palindrome for " + myNumber + " after ");
long palindrome = searchForPalindrome(myNumber);
System.out.println(" step(s) is " + palindrome);
}
}
}
You can use this code also to invert the digits of a number based on the octal system or any system based on a number from 2 up to 10. There's no error handling for numbers which can't have a palindrome. One possibility would be to abort after a certain number of steps.