:
dfact = lambda n: (n <= 0) or n * dfact(n-2)
, "" .
!!= (2 * k)!/(2 ** k * k!)
k = (n + 1)/2. n = 2k, , ,
!!= (2k)!!= 2 * k * k!.
, , , :
import math
fact = math.factorial
def dfact(n):
if n % 2 == 1:
k = (n+1)/2
return fact(2*k) / (2**k * fact(k))
else:
return 2**k * fact(k)
Now this code is obviously not very efficient for large n, but it is very instructive. More importantly, since we are now dealing with standard factorials, this is a very good starting point for optimization when working with really large numbers. Once you can try to use logarithms or gamma functions to obtain approximate double factorials for large numbers.
Karol source
share