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( "tapered" )
local fact2 = require( "fact2" )

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

t.done()

See also