# Palindrome Number in JavaScript

When the digits of a number are reversed and the value remains the same, then these types of numbers called palindrome numbers. In other words, it reads the same both forward and backward. Palindrome numbers have long piqued the interest of mathematicians and puzzle fans, and they have useful applications in several disciplines, including mathematics, computer science, and cryptography.

## History and Origin of Palindrome Numbers

Palindromic words and phrases, such as "Able were I ere I saw Elba," have been found in old manuscripts. Throughout history, people have been fascinated by palindromes, and they have become common in writing, word games, and puzzles.

## Applications and Use Cases

1. Palindromic numbers are used in number theory, combinatorics, and fun mathematical exercises. To gain a better grasp of number systems and arithmetic, mathematicians investigate the characteristics and patterns of palindromic numbers.

2. Palindrome numbers present intriguing programming issues in the field of computers. To assess logical reasoning and problem-solving abilities, they are frequently employed in coding exercises, algorithm creation, and interview questions.

3. The palindromic number uses can be found in cryptography techniques. For instance, some encryption methods use palindromic integers for both the encryption and decryption keys, which increases the complexity of the encryption procedure.

4. Palindromic numbers can be incorporated into puzzle games, mental exercises, and game dynamics. They can be used to develop engaging level layouts or produce random patterns based on palindromic characteristics.

5. Palindromes are a useful tool for data validation and error detection. For instance, they can be used to use palindromic checksum techniques to validate the integrity of data during transmission or storage.

Palindrome numbers offer certain benefits:

• Simple concept: Anyone with a rudimentary understanding of mathematics may understand the meaning of palindrome numbers.
• Versatility: Palindromes have a wide range of uses, from literature and wordplay to mathematics and computer science.

Palindrome Numbers Have Some Limitations:

• Palindromic qualities have limited applicability and are frequently restricted to particular fields, such as computer technology or mathematics.
• Numerical Constraints: As the size of the number rises, palindrome detection techniques become less effective. The computing complexity may become a limiting issue for really big numbers.

Examples

• 121: As it reads the same from left to right as it does from right to left, the number 121 is a palindrome.
• 3443: Another palindrome number is 3443, which has the same digits when reversed.
• 12321: This number has a mirror image structure and is also a palindrome.
• 1001: Although it has a leading zero, the number 1001 is nevertheless regarded as a palindrome.
• 1221: When turned over, the symmetrical number 1221 remains the same.

## Non-palindrome Numbers

Numerical values that do not read the same forwards and backward are known as non-palindrome numbers. They don't possess the symmetry that palindrome numbers do. Here are a few instances:

• 123: Since it appears differently when read backward, this number cannot be a palindrome.
• 456: The palindrome test is not passed by the number 456 because it is also not symmetric.
• 98789: Despite having repeating digits, the number 98789 is not a palindrome.
• 101: This number is not a palindrome despite the middle-repeated digit.
• 987654321: This large number does not pass the palindrome test because it is not symmetric.

## Properties of Palindrome Numbers

• Symmetry: Palindrome numbers exhibit symmetry, which defines them. When the digits of a palindrome number are switched around, the final number doesn't change. Examples of numbers with this feature are 121 and 3443.
• Mirror Images: Palindrome numbers can be considered mirror copies of one another. The first and second halves of the number are identical. For instance, the first half (12) of the palindrome number 12321 is the opposite of the second half (21).
• Odd and Even Digits: Palindrome numbers can have either an odd or an even number of digits. When the number is turned around, the middle digit(s) in both situations stay the same. For instance, 1221 has an even number of digits but the palindrome number 12321 has an odd number.
• Leading Zeros: The presence of leading zeros doesn't change the fact that a number is palindromic. For instance, despite having a zero as its starting point, the number 1001 is a palindrome.
• Repeated digits: Palindrome numbers can contain repeated digits. The repeat may take place in either the middle, at either end or in both positions. For instance, the digit 2 is repeated on both ends of the number 1221.
• Palindrome Chains: When palindrome numbers' digits are continuously added to one another, chains of palindromes can result. The resulting sums may be palindromic or not. One such palindrome chain is formed by the number 88: 88 16 17 88.
• Palindrome Primes: Prime numbers that can only be divided by one and themselves are known as palindrome primes. Some examples are 131 and 313.
• Different Bases Have Palindromic Qualities: Palindromic qualities are not just applicable to decimal numbers. Other number bases, such as binary (base 2), octal (base 8), and hexadecimal (base 16), also exhibit palindromes. One such palindrome is the binary number 101.

## Different Approaches for Palindrome Number

JavaScript's basic method of testing for palindrome numbers is flipping a number or string on its side and comparing the result to the original value.

### Naïve Approach

Reversing the Number/text: We must reverse a number or text to determine if it is a palindrome. The.toString() method in JavaScript can be used to turn a number into a string, and there are several ways to do the opposite, including turning the string into an array, using a loop, or using built-in string manipulation capabilities.

`function isPalindrome(value) {  // Convert value to a string for easier manipulation  const stringValue = value.toString();  // Reverse the string  const reversedString = stringValue.split('').reverse().join('');  // Compare the original and reversed strings  if (stringValue === reversedString) {    return true; // Palindrome  } else {    return false; // Not a palindrome  }}`

Comparison of the Original and Reversed: After obtaining the reversed value, we contrast it with the original string or number. A palindrome is one when the original value and the reversed value are the same.

### Limitations of the Naïve Approach

• Performance issues: For large integers or strings, using.split('').reverse().join('') can be inefficient. This method has an O(n) time complexity, where n is the length of the string. As a result, the execution time can dramatically increase for longer strings.
• Memory Usage: Making a reversed copy of the string and converting the number to a string uses more memory. This additional memory allocation might not always be desired, particularly when working with huge numbers or when memory optimization is important.
• Limited Applicability: Since the naive method compares reversed strings, it is most appropriate for textual palindromes. It might not be relevant in situations involving number structures or patterns that are more sophisticated.
• To compare characters in the original number or string without having to reverse the full value, the optimized method for testing palindrome values in JavaScript uses pointers or indices. This method avoids the need to make a reversed duplicate, which improves efficiency and uses less memory. We'll examine the steps in the optimized strategy in this part and talk about its benefits over the naive approach.
• Using Pointers or Indices: To compare characters from the start and finish of the original number or string, we can use two pointers or indices rather than reversing the full string. One pointer is typically at the beginning (index 0), and two more are at the end (index length - 1).
• Character Comparison: We evaluate the characters pointed out by the two pointers at each stage. Upon determining that they are equal, we move the first pointer up and move the second pointer down until they meet in the middle. We may determine that a number or string is not a palindrome if the letters are ever not equal.
`function isPalindrome(value) {  // Convert value to a string for easier manipulation  const stringValue = value.toString();  let start = 0;  let end = stringValue.length - 1;  while (start < end) {    if (stringValue[start] !== stringValue[end]) {      return false; // Not a palindrome    }    start++;    end--;  }  return true; // Palindrome}`

In comparison to the naive approach, the optimized approach has the following benefits:

• Better Performance: Since we don't reverse the entire string, the optimized approach's time complexity is O(n/2) rather than the naive approach's O(n), which makes it more effective. As the string length grows, the reduction in temporal complexity becomes more pronounced.
• Reduced Memory Usage: The optimized method just needs a small amount of extra memory to keep the indices or pointers, as opposed to the naive method, which makes a reversed copy of the string. When working with huge numbers or strings, this memory efficiency is advantageous since it prevents irrational memory allocation.
• Flexibility: The optimized method works with both textual and numeric palindromes. It is effective with all types of string data, including integers, letters, phrases, and sentences. It can handle a variety of palindrome conditions and is not restricted to reversing full strings.

Overall, the optimized method achieves a compromise between simplicity and performance. It gives a quick approach to finding palindrome numbers or strings and avoids the need for pointless string manipulation. It's important to keep in mind, though, that the optimized solution assumes the input is a legitimate string or number and can need further checking or handling for unique situations or edge scenarios.

## Recursive Approach

An alternate method to determine whether a given integer or string is a palindrome is to use JavaScript's recursive method for checking palindrome numbers. With this strategy, the idea of recursion is used to divide the problem into smaller subproblems and solve them successively.

• Palindrome-checking Recursive Function: To put the recursive technique into practice, we develop a recursive function. This function compares characters or digits recursively using the initial number or string as a parameter.
• Base Case: The recursive function has to specify a base case, or the most basic version of the issue that can be resolved instantly. The base case for palindrome checks often entails a comparison of a single character or number.
• Recursive Calls: When using a recursive function, we make recursive calls to the function itself, sending smaller subproblems along the way. The function runs a first and final character or digit comparison before calling itself recursively with the remainder of the substring or integer.
`function isPalindrome(value) {  // Convert value to a string for easier manipulation  const stringValue = value.toString();  function checkPalindrome(start, end) {    // Base case: Single character or digit    if (start >= end) {      return true;    }    // Compare characters/digits    if (stringValue[start] !== stringValue[end]) {      return false; // Not a palindrome    }    // Recursive call with smaller subproblem    return checkPalindrome(start + 1, end - 1);  }  return checkPalindrome(0, stringValue.length - 1);}`

## Time and Space Complexity Analysis

### Iterative Approach

This method compares a number's digits starting from either end until it reaches the center. Here is the evaluation:

• Time Complexity: The iterative approach has an O(n) time complexity, where n is the number of digits in the inputted number. Up until the middle digit is reached, we compare two digits, one from the start and the other from the finish, in each iteration. The needed number of comparisons grows linearly with the number of digits.
• Space Complexity: Since no additional space is needed, the iterative approach's space complexity is O(1). Regardless of the amount of input, we only require a small number of variables to hold transient values during the comparison process.

### Recursive Approach

The recursive method compares digits from both ends of the integer using a recursive function. Here is the evaluation:

• Time Complexity: The recursive method's temporal complexity is T(n) = T(n-2) + O(1), where n is the number of digits in the inputted number. We compare two digits in each recursive call before making another call with the remaining digits from both ends. This continues until only 0 or 1 digits are remaining to compare, at which point the base case is reached. With each iteration, the number of recursive calls drops by 2. As a result, the temporal complexity is roughly O(n/2), which reduces to O(n). Even if there are fewer recursive calls, the complexity as a whole is still linear.
• Space Complexity: The depth of the recursion, which is controlled by the number of digits, affects how complicated the space is in the recursive approach. A new stack frame is constructed for the function's variables and returns the address with each iterative call. As a result, the recursive approach's space complexity is O(n), where n is the number of digits. The depth of the recursion, which is inversely proportional to the input number's digit count, causes an increase in space consumption.