HP Forums
Double factorial - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Double factorial (/thread-3240.html)



Double factorial - salvomic - 03-02-2015 01:49 PM

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


RE: Double factorial - Han - 03-02-2015 04:18 PM

(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 \) )


RE: Double factorial - Helge Gabert - 03-02-2015 04:36 PM

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)?


RE: Double factorial - salvomic - 03-02-2015 05:15 PM

(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


RE: Double factorial - Gerald H - 03-02-2015 05:30 PM

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
;
;


RE: Double factorial - salvomic - 03-02-2015 05:32 PM

(03-02-2015 05:30 PM)Gerald H Wrote:  An assist from the 50G side:
...

thanks!
50g is always a great Calc!


RE: Double factorial - DrD - 03-03-2015 12:02 PM

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-


RE: Double factorial - salvomic - 03-03-2015 01:24 PM

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

...
-Dale-

nice, Dale, thanks!


RE: Double factorial - salvomic - 03-03-2015 06:15 PM

(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