Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is their a way to add factorials in ROBLOX?

Asked by 8 years ago

Does roblox support factorials?

For example;

print(5!) 

Should print 120

1 answer

Log in to vote
2
Answered by 8 years ago

There are currently no arithmetic operators or functions in the math library to calculate this for us. We'd have to create this function ourselves, but this shouldn't be too much of a problem. Here's how it would work:

-- Arguments:
-- 1. "n" - the factorial number to calculate

local function factorial(n)
    local total = 1 -- the current product of the factorial
    for i = 1,n do -- iterations of the number given
        total = total * i -- increase the product
    end -- end
    return total -- return the final answer
end

print(factorial(5)) -- > 120
print(factorial(4)) -- > 24

Hope that helped, if you need a bit more of an explanation on how this function works, just leave a comment and I'll get to it.

Ad

Answer this question