Tuesday, May 30, 2023
HomeSoftware EngineeringPalindrome chain size in Java

# Palindrome chain size in Java

## The problem#

Quantity is a palindrome if it is the same as the quantity with digits in reversed order. For instance, `5``44``171``4884` are palindromes, and `43``194``4773` are usually not.

Write a operate which takes a constructive integer and returns the variety of particular steps wanted to acquire a palindrome. The particular step is: “reverse the digits, and add to the unique quantity”. If the ensuing quantity just isn’t a palindrome, repeat the process with the sum till the ensuing quantity is a palindrome.

If the enter quantity is already a palindrome, the variety of steps is “.

All inputs are assured to have a ultimate palindrome which doesn’t overflow `lengthy`.

### Instance#

For instance, begin with `87`:

``````  87 +   78 =  165     - step 1, not a palindrome
165 +  561 =  726     - step 2, not a palindrome
726 +  627 = 1353     - step 3, not a palindrome
1353 + 3531 = 4884     - step 4, palindrome!
``````

`4884` is a palindrome and we would have liked `4` steps to acquire it, so reply for `87` is `4`.

Some fascinating data on the issue might be discovered on this Wikipedia article on Lychrel numbers.

## The answer in Java code#

Possibility 1:

``````public class Palindromes {

public static int palindromeChainLength (lengthy n) {
String ns = "" + n, nrs = "" + new StringBuilder(ns).reverse();
return ns.equals(nrs) ? 0 : 1 + palindromeChainLength(n + Lengthy.valueOf(nrs));
}

}
``````

Possibility 2:

``````public class Palindromes {
public static int palindromeChainLength(lengthy n) {
int step = 0;
whereas (n != reverseNumber(n)) {
n = n + reverseNumber(n);
step++;
}
return step;
}

non-public static lengthy reverseNumber(lengthy n) {
char[] quantity = String.valueOf(n).toCharArray();
String reverseNumber = "";
for (int depend = quantity.size - 1; depend >= 0; count--) {
reverseNumber = reverseNumber + quantity[count];
}
return Lengthy.parseLong(reverseNumber);
}
}
``````

Possibility 3:

``````public class Palindromes {
public static int palindromeChainLength (lengthy n) {
String s = String.valueOf(n);
String r = new StringBuilder(String.valueOf(n)).reverse().toString();
if (s.equals(r)) return 0;
return 1 + palindromeChainLength(n + Lengthy.parseLong(r));
}
}
``````

## Check circumstances to validate our answer#

``````import org.junit.Check;
import static org.junit.Assert.*;

public class PalindromesTest {

@Check
public void testPalindrome() {
assertEquals(0, Palindromes.palindromeChainLength(1));
assertEquals(0, Palindromes.palindromeChainLength(88));
assertEquals(0, Palindromes.palindromeChainLength(393));
}

@Check
public void testNonPalindrome() {
assertEquals(1, Palindromes.palindromeChainLength(10));
assertEquals(1, Palindromes.palindromeChainLength(134));
assertEquals(4, Palindromes.palindromeChainLength(87));
assertEquals(7, Palindromes.palindromeChainLength(2897));
assertEquals(24, Palindromes.palindromeChainLength(89));
}

}
``````
RELATED ARTICLES