Palindromic Number in Mathematica

Published on 18 January 2023 (Updated: 18 January 2023)

Welcome to the Palindromic Number in Mathematica page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

(* Code *)

(* This is provided by a Mathematica built-in PalindromeQ.  Only a function to provide the 'user interface' is needed: *)

palindromicNumberMain = s \[Function] Module[
    {e = "Usage: please input a non-negative integer"},
    Catch[
     Replace[
      PalindromeQ@
       (* convert string to integer, or throw *)
       If[StringMatchQ[s, DigitCharacter ..],
        FromDigits[s],
        Throw[e]],
      {False -> "false", True -> "true"}]]];


(* Valid Tests *)

Print /@ palindromicNumberMain /@ {
    "7",
    "2442",
    "232",
    "5215",
    "521"
    };


(* Invalid Tests *)

palindromicNumberMain[""]
palindromicNumberMain["a"]
palindromicNumberMain["-5"]
palindromicNumberMain["5.41"]

Palindromic Number in Mathematica was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.