``factorial(n)`` function (#3253)

* factorial(n) function

* Use local function

* Woops

* Return NaN if the number is less than 0

* Update function

Co-authored-by: thegrb93 <grbrown93@sbcglobal.net>

---------

Co-authored-by: thegrb93 <grbrown93@sbcglobal.net>
This commit is contained in:
Astralcircle 2025-01-30 11:49:35 +03:00 committed by GitHub
parent 158ec276c6
commit c75ee43ff1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View File

@ -303,6 +303,18 @@ end
--[[************************************************************************]]--
__e2setcost(10)
[nodiscard]
e2function number factorial(number n)
if n < 0 then return 0 / 0 end
if n > 170 then return math.huge end
local res = 1
for i = 2, n do res = res * i end
return res
end
__e2setcost(2) -- approximation
[nodiscard]

View File

@ -54,6 +54,7 @@ E2Helper.Descriptions["isinf(n)"] = "Returns 1 if given value is a positive infi
E2Helper.Descriptions["isnan(n)"] = "Returns 1 if given value is not a number (NaN); otherwise 0."
E2Helper.Descriptions["inf()"] = "Returns a huge constant (infinity)"
E2Helper.Descriptions["mod(nn)"] = "Modulo, returns the Remainder after Argument 1 has been divided by Argument 2. Note \"mod(-1, 3) = -1\""
E2Helper.Descriptions["factorial(n)"] = "Returns the Factorial of the Argument"
E2Helper.Descriptions["sqrt(n)"] = "Returns the Square Root of the Argument"
E2Helper.Descriptions["cbrt(n)"] = "Returns the Cube Root of the Argument"
E2Helper.Descriptions["root(nn)"] = "Returns the Nth Root of the first Argument"