Refactored palindrome math function into a reusable function