Ask Your Question

How to calculate the double factorial in SageMath?

asked 2017-08-14 23:03:49 -0500

Fusion809 gravatar image

updated 2017-08-16 20:55:48 -0500

I would like to implement the square root of 2 power series:

$ \sqrt{2} = -\sum \limits_{n=0}^{\infty}{\frac{(-1)^n (2n-3)!!}{2^n \times n!}} $

(which I obtained from the Maclaurin series of $\sqrt{1+x}$ with $x=1$) in SageMath but I cannot seem to find the double factorial function in the SageMath docs. Is there one? I suppose if a double factorial function is not available I could use this method of finding the double factorial that I found on Wolfram MathWorld:

$\Gamma(n+\frac{1}{2}) = \frac{(2n-1)!!}{2^n}\sqrt{\pi}$

edit retag flag offensive close merge delete


In my opinion the series is slowly converging to the limit, for instance:

sage: R = RealField(250)
sage: R(sqrt(2))
sage: taylor( sqrt(1+x), x, 0, 400 ).subs( x=R(1) )
sage: coeffs = taylor( sqrt(1+x), x, 0, 400 ).coefficients()
sage: R( coeffs[399][0] )
sage: R( coeffs[400][0] )

The Taylor polynomial contains the needed coefficients. However even after $400$ terms we get relatively big numbers to add, subtract, add...

dan_fulea gravatar imagedan_fulea ( 2017-08-16 21:25:48 -0500 )edit

Ya I'm well aware convergence is an issue. This method with N (number of terms) = 10,000 is only accurate to 6 decimal places. I know a far more convergent method of estimating $\sqrt{2}$ is Newton's method. Just wanted to give this a try.

Fusion809 gravatar imageFusion809 ( 2017-08-16 21:34:18 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2017-08-15 18:17:21 -0500

nbruin gravatar image

There's the method multifactorial(2) on integers:

sage: 6.multifactorial(2)
sage: 7.multifactorial(2)

You might want to check the definition used there fits your needs.

edit flag offensive delete link more

answered 2017-08-15 05:57:01 -0500

dan_fulea gravatar image

updated 2017-08-15 05:58:09 -0500

One quick(ly found) possibility would be to import factorial2 from sympy, sample code:

sage: from sympy import factorial2
sage: factorial2(99)
sage: factorial(100) / factorial(50) / 2^50
sage: ?factorial2

Note that factorial2 is defined for both odd and even natural numbers. (Also for negative odd ones.)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-08-14 23:03:49 -0500

Seen: 143 times

Last updated: Aug 16 '17