Double Factorial

170 views
Skip to first unread message

Jason Merrill

unread,
Aug 26, 2008, 8:34:11 PM8/26/08
to sage-support
I couldn't find a double factorial function in sage. That is,

n!! == n*(n - 2)*(n - 4)...

Does Sage have a double factorial somewhere that I'm missing. If not,
could it?

Regards,

JM

William Stein

unread,
Aug 26, 2008, 9:20:56 PM8/26/08
to sage-s...@googlegroups.com

I had to write this for Pynac. Here's the 1-liner that does it...

def doublefactorial(x):
n = Integer(x)
if n < -1:
raise ValueError, "argument must be >= -1"
return prod([n - 2*i for i in range(n//2)])

sage: doublefactorial(20)
3715891200

Robert Bradshaw

unread,
Aug 26, 2008, 9:37:36 PM8/26/08
to sage-s...@googlegroups.com

Yes, Integers have a (highly optimized) multifactorial method.

sage: a = 20

sage: a.multifactorial(2)
3715891200

- Robert

Jason Merrill

unread,
Aug 26, 2008, 10:25:34 PM8/26/08
to sage-support
On Aug 26, 9:37 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:
> On Tue, 26 Aug 2008, William Stein wrote:
>
> > On Tue, Aug 26, 2008 at 5:34 PM, Jason Merrill <jwmerr...@gmail.com> wrote:

> >> Does Sage have a double factorial somewhere that I'm missing.  If not,
> >> could it?
>
> Yes, Integers have a (highly optimized) multifactorial method.
>
> sage: a = 20
>
> sage: a.multifactorial(2)
>   3715891200
>
> - Robert

Perfect. Thanks.

JM
Reply all
Reply to author
Forward
0 new messages