From ad9f2fdca4e2860117d3f5e574a66d428ade8ddf Mon Sep 17 00:00:00 2001 From: HybridDog Date: Tue, 5 Mar 2019 10:11:21 +0100 Subject: [PATCH] Add math.factorial (#8298) --- builtin/common/misc_helpers.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index 16c497156..e8912e57a 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -243,6 +243,20 @@ function math.sign(x, tolerance) return 0 end +-------------------------------------------------------------------------------- +function math.factorial(x) + assert(x % 1 == 0 and x >= 0, "factorial expects a non-negative integer") + if x >= 171 then + -- 171! is greater than the biggest double, no need to calculate + return math.huge + end + local v = 1 + for k = 2, x do + v = v * k + end + return v +end + -------------------------------------------------------------------------------- function get_last_folder(text,count) local parts = text:split(DIR_DELIM)