Methods Summary |
---|
static long | findPalindrome(long num)find a palindromic number given a starting point, by
calling ourself until we get a number that is palindromic.
if (num < 0)
throw new IllegalStateException("negative");
if (isPalindrome(num))
return num;
if (verbose)
System.out.println("Trying " + num);
return findPalindrome(num + reverseNumber(num));
|
static boolean | isPalindrome(long num)Check if a number is palindromic.
// Consider any single digit to be as palindromic as can be
if (num >= 0 && num <= 9)
return true;
int nDigits = 0;
while (num > 0) {
digits[nDigits++] = num % 10;
num /= 10;
}
for (int i=0; i<nDigits/2; i++)
if (digits[i] != digits[nDigits - i - 1])
return false;
return true;
|
public static void | main(java.lang.String[] argv)
for (int i=0; i<argv.length; i++)
try {
long l = Long.parseLong(argv[i]);
if (l < 0) {
System.err.println(argv[i] + " -> TOO SMALL");
continue;
}
System.out.println(argv[i] + "->" + findPalindrome(l));
} catch (NumberFormatException e) {
System.err.println(argv[i] + "-> INVALID");
} catch (IllegalStateException e) {
System.err.println(argv[i] + "-> " + e);
}
|
static long | reverseNumber(long num)
int nDigits = 0;
while (num > 0) {
digits[nDigits++] = num % 10;
num /= 10;
}
long ret = 0;
for (int i=0; i<nDigits; i++) {
ret *= 10;
ret += digits[i];
}
return ret;
|