fact

function fact( x ) --> res

Description

Returns the factorial of x. The factorial of x is equal to 1*2*3*…* x.

Parameters

x

The nonnegative number for which you want the factorial. If number is not an integer, it is truncated.

Return Values

res

The factorial of x.

Code

--ZFUNC-fact-v1
local function fact( x ) --> res
   if x < 0 then return math.sqrt( x ) end
   x = math.floor( x )

   if x == 0 then
      return 1
   else
      return x * fact( x-1 )
   end
end

return fact

Examples

local t = require( "tapered" )
local fact = require( "fact" )

t.is( 120, fact( 5 ) )
t.is( 1, fact( 1.9 ) )
t.is( 1, fact( 0 ) )

t.done()

See also