Hey friends, is there an efficient implementation of double factorial somewhere? I implemented this one (any tips on improving its performance are welcome):
function doublefactorial(number)
fact = one(number)
if number % 2 == 0
for m = 1:number
if m % 2 == 0
fact *= m
end
end
elseif number % 2 == 1
for m = 1:number
if m % 2 == 1
fact *= m
end
end
end
return fact
end
I don’t think you need to worry too much about performance here — if you’re using Int64s, this will overflow in 33 operations! But here’s a slight simplification:
function doublefactorial(number)
fact = one(number)
for m in iseven(number)+1:2:number
fact *= m
end
return fact
end
Optimization would really only matter if you’re talking about BigInts — and then you’d have to delve into GMP internals to really do anything of note.
(This is a classic case where declaring the argument as number::Integer is a good idea, since otherwise it will silently given the wrong result for non-integer inputs like 1.5.)