There actually exists a (in my opion) better factorial formula that extends it to non-integers, and in my opinion, is actually really elegant! You just have to plug a high value for N, and you have an extended definiton for factorials! Here it is:
If you’re confused about the pi symbol a bit, that just means you’re starting at k=1, and multiply it by itself except k=2, and then multiply by itself except k=3, and so on until k=N, and since N is meant to get bigger and bigger until it reaches “infinity”, that process will never end. Of course we can’t handle a non-terminating process, but we can get
close to it. If we set N=10,000, we will get a VERY close result to the actual factorial function.
Now, we can code this pretty easily in Lua, just use a
for loop and stuff.
Also, this also works for negative numbers as well, just set
x to a negative number. BUT, there’s a catch.
This does not work for negative
whole numbers.
Why?
If you try using the original definition of the factorial you will end up that to get the previous factorial, for example I’ll just use that 4!=24 and find 3! using that method. To get the previous factorial, you have to divide by the number you are taking the previous factorial of.
Example: 4!=24, so divide by 4.
24/4=6
So 3! is 6, which is indeed the correct answer!
However, if you try this using that 0!=1, you have to divide by 0, which you cannot do.
So that’s why I said this doesn’t work for negative whole numbers.
I don"t think negative factorials have a use though, (atleast one that I know of)
So yeah, the product formula is my favourite extension of my factorials for non-integers!
And there’s something bonus about it!
This formula actually gives the correct value for x!, not (x-1)!
Edit: I implemented the formula onto Lua!
factorial = 5 -- number you want to approximate the factorial of
approx = 1000000 -- higher values lead to more accurate results
temp = 1 -- dont change this
for i = 1, approx do
temp = temp * (i/(factorial + i))
end
wait(0)
result = approx^factorial * temp
print("Result = " .. result)
Here’s the .rbxl file:
factorial.rbxl (44.9 KB)