Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is their a way to add factorials in ROBLOX?

Does roblox support factorials?

For example;

print(5!) 

Should print 120

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.