A PHP Function to Compute the Double Factorial of an Integer
Using Arbitrary-Precision Arithmetic
PHP Function by Jay Tanner - 2017


The Double Factorial

Note 1:
The double factorial of $N$ $(N~!!)$ should not be confused with taking the factorial of a factorial.

Double factorial of $N ~=~ N~!! ~\ne~ (N!)!$

The factorial of $N$ factorial $= (N!)!$  and is a different thing entirely, although it is related.

The double factorial of positive integer $(N)$ is defined as the product of every other positive
integer with the same parity as $N$ in the range from $1$ up to and including $N$.  The parity of
an integer simply refers to whether it is an odd or an even number.

Note 2:
The double factorial values of $0$ and $-1$ are equated to $1$ by definition, but double factorial
values for integers that are less than $-1$ are not defined.  With the definitive exceptions of
the cases where ($N = -1$) and ($N=0$), the following simple rules apply.


For even number $N$:

$\begin{align*} (1)~~~~~~~~ N~!! ~=~ \prod_{n ~=~ 1}^\frac{N}{2} (2n) ~=~ N\cdot(N-2)\cdot(N-4)\cdot(N-6)\cdot ~...~ \cdot(4)\cdot(2)\cdot(1) \end{align*}$

The double factorial of even number $N$ is the product of $1$ and all of the even integers up to $N$.

EXAMPLE:
$12~!! ~=~ 1\cdot2\cdot4\cdot6\cdot8\cdot10\cdot12 ~=~ 46080$


For odd number $N$:

$\begin{align*} (2)~~~~~~~~ N~!! ~=~ \prod_{n ~=~ 1}^\frac{N+1}{2} (2n-1) ~=~ N\cdot(N-2)\cdot(N-4)\cdot(N-6)\cdot ~...~ \cdot(5)\cdot(3)\cdot(1) \end{align*}$

The double factorial of odd number $N$ is the product of $1$ and all of the other odd integers up to $N$.

EXAMPLE:
$15~!! ~=~ 1\cdot3\cdot5\cdot7\cdot9\cdot11\cdot13\cdot15 ~=~ 2027025$


For any integer $N$ in general, both of the above equations can be combined into one:

$\begin{align*} (3)~~~~~~~~ N~!! ~=~ \prod_{n ~=~ 0}^{\lceil\frac{N}{2}\rceil-1} (N - 2n) ~=~ N\cdot(N-2)\cdot(N-4)\cdot(N-6)\cdot ~...~ \end{align*}$


The following PHP function will compute the arbitrary-precision double factorial value for any given valid integer argument and is an implementation of equation $(3)$ above.



As the following table of double factorials demonstrates, the numbers quickly grow very large.  The table was computed using the above PHP function and it only took a couple of seconds.
(N!!) Double Factorial Table for N = -1 to 100

 -1!! = 1
  0!! = 1
  1!! = 1
  2!! = 2
  3!! = 3
  4!! = 8
  5!! = 15
  6!! = 48
  7!! = 105
  8!! = 384
  9!! = 945
 10!! = 3840
 11!! = 10395
 12!! = 46080
 13!! = 135135
 14!! = 645120
 15!! = 2027025
 16!! = 10321920
 17!! = 34459425
 18!! = 185794560
 19!! = 654729075
 20!! = 3715891200
 21!! = 13749310575
 22!! = 81749606400
 23!! = 316234143225
 24!! = 1961990553600
 25!! = 7905853580625
 26!! = 51011754393600
 27!! = 213458046676875
 28!! = 1428329123020800
 29!! = 6190283353629375
 30!! = 42849873690624000
 31!! = 191898783962510625
 32!! = 1371195958099968000
 33!! = 6332659870762850625
 34!! = 46620662575398912000
 35!! = 221643095476699771875
 36!! = 1678343852714360832000
 37!! = 8200794532637891559375
 38!! = 63777066403145711616000
 39!! = 319830986772877770815625
 40!! = 2551082656125828464640000
 41!! = 13113070457687988603440625
 42!! = 107145471557284795514880000
 43!! = 563862029680583509947946875
 44!! = 4714400748520531002654720000
 45!! = 25373791335626257947657609375
 46!! = 216862434431944426122117120000
 47!! = 1192568192774434123539907640625
 48!! = 10409396852733332453861621760000
 49!! = 58435841445947272053455474390625
 50!! = 520469842636666622693081088000000
 51!! = 2980227913743310874726229193921875
 52!! = 27064431817106664380040216576000000
 53!! = 157952079428395476360490147277859375
 54!! = 1461479318123759876522171695104000000
 55!! = 8687364368561751199826958100282265625
 56!! = 81842841814930553085241614925824000000
 57!! = 495179769008019818390136611716089140625
 58!! = 4746884825265972078944013665697792000000
 59!! = 29215606371473169285018060091249259296875
 60!! = 284813089515958324736640819941867520000000
 61!! = 1782151988659863326386101665566204817109375
 62!! = 17658411549989416133671730836395786240000000
 63!! = 112275575285571389562324404930670903477890625
 64!! = 1130138339199322632554990773529330319360000000
 65!! = 7297912393562140321551086320493608726062890625
 66!! = 74589130387155293748629391052935801077760000000
 67!! = 488960130368663401543922783473071784646213671875
 68!! = 5072060866326559974906798591599634473287680000000
 69!! = 33738248995437774706530672059641953140588743359375
 70!! = 355044260642859198243475901411974413130137600000000
 71!! = 2395415678676082004163677716234578672981800778515625
 72!! = 25563186766285862273530264901662157745369907200000000
 73!! = 174865344543353986303948473285124243127671456831640625
 74!! = 1891675820705153808241239602722999673157373132800000000
 75!! = 13114900840751548972796135496384318234575359262373046875
 76!! = 143767362373591689426334209806947975159960358092800000000
 77!! = 1009847364737869270905302433221592504062302663202724609375
 78!! = 11213854265140151775254068364941942062476907931238400000000
 79!! = 79777941814291672401518892224505807820921910393015244140625
 80!! = 897108341211212142020325469195355364998152634499072000000000
 81!! = 6462013286957625464523030270184970433494674741834234775390625
 82!! = 73562883979319395645666688474019139929848516028923904000000000
 83!! = 536347102817482913555411512425352545980058003572241486357421875
 84!! = 6179282254262829234236001831817607754107275346429607936000000000
 85!! = 45589503739486047652209978556154966408304930303640526340380859375
 86!! = 531418273866603314144296157536314266853225679792946282496000000000
 87!! = 3966286825335286145742268134385482077522528936416725791613134765625
 88!! = 46764808100261091644698061863195655483083859821779272859648000000000
 89!! = 352999527454840466971061863960307904899505075341088595453568994140625
 90!! = 4208832729023498248022825567687608993477547383960134557368320000000000
 91!! = 32122956998390482494366629620388019345854961856039062186274778466796875
 92!! = 387212611070161838818099952227260027399934359324332379277885440000000000
 93!! = 2987435000850314871976096554696085799164511452611632783323554397412109375
 94!! = 36397985440595212848901395509362442575593829776487243652121231360000000000
 95!! = 283806325080779912837729172696128150920628587998105114415737667754150390625
 96!! = 3494206602297140433494533968898794487257007658542775390603638210560000000000
 97!! = 27529213532835651545259729751524430639300973035816196098326553772152587890625
 98!! = 342432247025119762482464328952081859751186750537191988279156544634880000000000
 99!! = 2725392139750729502980713245400918633290796330545803413734328823443106201171875
100!! = 34243224702511976248246432895208185975118675053719198827915654463488000000000000

This is the PHP source code for the above table, including the double factorial function code.