Factorial in Mathematica

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

Welcome to the Factorial 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 *)

(* The Mathematica built-in Factorial essentially provides the required behavior;
so the only code needed is the 'user interface' to make it look a little more
like a Unix command-line tool: *)

factorialMain = Catch[
    (* convert string to integer, or throw *)
    If[
     StringMatchQ[#, DigitCharacter ..],
     FromDigits[#]!,
     Throw["Usage: please input a non-negative integer"]]] &;


(* Valid Tests *)

Print /@ factorialMain /@ {"0", "1", "4", "8", "10"};

(* Note that Mathematica (as far as I know) doesn't impose an upper limit on computations: *)
factorialMain["10000"]


(* Invalid Tests *)

factorialMain[""]
factorialMain["asdf"]
factorialMain["-1"]

Factorial 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.