HP Forums

Full Version: Double factorial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi all,
how is a simple (and precise) way to define a command to calculate the "double factorial" (see here) with Prime?
I mean to use it as a simple command.
We cannot write n!!, as that is not the same...

thank you,
Salvo
(03-02-2015 01:49 PM)salvomic Wrote: [ -> ]hi all,
how it a simple (and precise) way to define a command to calculate the "double factorial" (see here) with Prime?
I mean to use it as a simple command.
We cannot write n!!, as that is not the same...

thank you,
Salvo

The wiki explains how you can calculate it using the regular factorial:

If \( n \) is even, then there exists some integer \( k \) such that \( n = 2k \).
\[ n!! = n(n-2)(n-4) \dotsm 4 \cdot 2 = (2k)(2k-2)(2k-4)\dotsm (2\cdot 2) \cdot (2\cdot 1)
= 2^k k! \]

If \( n \) is odd, then there exists some integer \( k \) such that \( n = 2k+1 \). Moreover, \( n-1 \) is even.
\[ n!! =
\frac{\text{product of all integers from 1 to } n}{\text{product of all even integers from 2 to } n-1} =
\frac{n(n-1)(n-2)\dotsm 3 \cdot 2 \cdot 1}{ (n-1)(n-3)(n-5)\dotsm 4 \cdot 2} =
\frac{n!}{(n-1)!!} = \frac{(2k+1)!}{2^k k!} \]

It should be pretty easy to write your own program using these formulas. (Note: the wiki has a slight error with respect to the formula for odd values of \(n \) )
No error checking, but something along these lines

#cas
DFA(n):=
BEGIN
IF odd(n) THEN
product(n,n,1,n,2);
ELSE
product(n,n,2,n,2);
END;
END;
#end

Is this what you are asking (simple program)?
(03-02-2015 04:18 PM)Han Wrote: [ -> ]The wiki explains how you can calculate it using the regular factorial:
...
It should be pretty easy to write your own program using these formulas. (Note: the wiki has a slight error ...

(03-02-2015 04:36 PM)Helge Gabert Wrote: [ -> ]No error checking, but something along these lines

#cas
...
Is this what you are asking (simple program)?

yes, thanks both!
so this should be enough "precise" and it is ok both in Home and CAS:
Code:

EXPORT factorial(n)
//Double factorial n!!
BEGIN
local f;
IF odd(n) THEN f:=product(X,X,,1,n,2);
ELSE f:=product(X,X,2,n,2); END;
RETURN(f);
END;

My memory failed, and I didn't remember "product()" Smile

thanks again,
Salvo
An assist from the 50G side:

::
CK1&Dispatch
BINT1
::
COERCEDUP
BINT2
#/
DUP
FPTR2 ^#FACT
Z2_
ROT
FPTR2 ^PPow#
FPTR2 ^RMULText
SWAP
#0=case
SWAPDROP
SWAP
FPTR2 ^#FACT
FPTR2 ^SWAPRDIV
;
;
(03-02-2015 05:30 PM)Gerald H Wrote: [ -> ]An assist from the 50G side:
...

thanks!
50g is always a great Calc!
Here is a slight variation on your theme:

Code:

EXPORT factorial(n)
//Double factorial n!!
BEGIN
RETURN IFTE(odd(n), product(X,X,1,n,2), product(X,X,2,n,2));
END;

-Dale-
(03-03-2015 12:02 PM)DrD Wrote: [ -> ]Here is a slight variation on your theme:

...
-Dale-

nice, Dale, thanks!
(03-02-2015 05:03 PM)compsystems Wrote: [ -> ]Please HP´ TEAM can include product operator in the next version of ROM, or at least that on the historial, the product command is displayed as an operator...

I agree.
∏, Productoria (or, Italian, Produttoria) as operator, a part of the command product: it would be very useful also.
Tim, do you think that it's possible to do it?

Thank you.

Salvo
Reference URL's