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( "taptest" )
local fact = require( "fact" )

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

t()

See also