fact2

function fact2( x ) --> res

Description

Returns the double factorial of x.

Parameters

x

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

Return Values

res

The double factorial of x.

Code

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

   if x == 0 or x == 1 then
      return 1
   else
      return x * fact2( x-2 )
   end
end

return fact2

Examples

local t = require( "taptest" )
local fact2 = require( "fact2" )

t( fact2( 6 ), 48 )
t( fact2( 7 ), 105 )
t( fact2( 0 ), 1 )

t()

See also