# Is there an efficient implementation of double factorial somewhere?

**URL:** <https://discourse.julialang.org/t/is-there-an-efficient-implementation-of-double-factorial-somewhere/104002>\
**Category:** General Usage\
**Created:** [September 18, 2023, 9:44pm UTC](https://discourse.julialang.org/t/is-there-an-efficient-implementation-of-double-factorial-somewhere/104002 "2023-09-18T21:44:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leticia-maria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leticia-maria/32/30981_2.png) [@Leticia-maria](https://discourse.julialang.org/u/Leticia-maria)\
**Post date:** [September 18, 2023, 9:44pm UTC](https://discourse.julialang.org/t/is-there-an-efficient-implementation-of-double-factorial-somewhere/104002/1 "2023-09-18T21:44:26Z")

</div>

Hey friends, is there an efficient implementation of `double factorial` somewhere? I implemented this one (any tips on improving its performance are welcome):

```julia
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

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 18, 2023, 9:53pm UTC](https://discourse.julialang.org/t/is-there-an-efficient-implementation-of-double-factorial-somewhere/104002/2 "2023-09-18T21:53:19Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [September 18, 2023, 9:58pm UTC](https://discourse.julialang.org/t/is-there-an-efficient-implementation-of-double-factorial-somewhere/104002/3 "2023-09-18T21:58:55Z")

</div>

If you hate typing:

```julia
using Base.Iterators
function df2(n)
    foldl(Base.:*, range(n, 1, step=-2))
end

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 18, 2023, 10:03pm UTC](https://discourse.julialang.org/t/is-there-an-efficient-implementation-of-double-factorial-somewhere/104002/4 "2023-09-18T22:03:17Z")

</div>

Of course, it’s `df3(n) = prod(n:-2:1)`! Even better, it looks like Base already has the GMP-internals optimizations you’d want here. 🙂

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 19, 2023, 1:14am UTC](https://discourse.julialang.org/t/is-there-an-efficient-implementation-of-double-factorial-somewhere/104002/5 "2023-09-19T01:14:20Z")

</div>

> [@mbauman](#):
>
> `function doublefactorial(number)`

(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`.)
