# Factorial in Lua

Published on 28 April 2022 (Updated: 05 February 2023) Welcome to the Factorial in Lua page! Here, you’ll find the source code for this program as well as a description of how the program works.

## Current Solution

``````--
-- Calculate the factorial
--
factorial = function(num)
product = 1
if num > 1
then
for i = 2, num
do
product = product * i
end
end
return product
end

--
-- Main executable logic
--
main = function(input)
maxInput = 20
usage = "Usage: please input a non-negative integer"

if not (input == nil or input == "")
then
inputValidation = input:gsub('[0-9]','')
if inputValidation:len() == 0
then
inputNum = tonumber(input)
if inputNum > maxInput
then
print('Input of ' .. inputNum .. ' is too large to calculate a factorial for. Max input is ' .. maxInput .. '.')
else
print(factorial(tonumber(input)))
end
else
print(usage)
end
else
print(usage)
end

end

-- Run the script
main(arg)
``````

Factorial in Lua was written by:

• Jeremy Grifski
• Matt Wiley

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

Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 15 2020 19:22:39. The solution was first committed on Oct 10 2019 12:43:45. As a result, documentation below may be outdated.

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