-
-
Notifications
You must be signed in to change notification settings - Fork 547
Expand file tree
/
Copy pathdouble_factorial_iterative.ts
More file actions
28 lines (25 loc) · 1.11 KB
/
double_factorial_iterative.ts
File metadata and controls
28 lines (25 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @function DoubleFactorialIterative
* @description Calculate the double factorial of a number (iterative implementation)
* @summary In mathematics, double factorial of a number n is denoted by n!!.
* It is not to be confused with (n!)!, which is the factorial function iterated twice.
* The double factorial is the product of all positive integers upto n that have the same parity (odd or even)
* as n.
* Therefore,
* 9!! = 9 . 7 . 5 . 3 . 1
* 10!! = 10 . 8 . 6 . 4 . 2
*
* Please note that for factorials of even numbers, the series ends at 2.
* @see [Wikipedia](https://en.wikipedia.org/wiki/Double_factorial)
* @see [Mathworld](https://mathworld.wolfram.com/DoubleFactorial.html)
* @see [GeeksForGeeks](https://www.geeksforgeeks.org/double-factorial/)
* @example DoubleFactorialIterative(4) = 8
* @example DoubleFactorialIterative(5) = 15
*/
const DoubleFactorialIterative = (n: number) => {
if (n < 0) throw new RangeError('The number needs to be non-negative')
let doubleFactorial = 1
for (let i = n; i > 0; i -= 2) doubleFactorial *= i
return doubleFactorial
}
export { DoubleFactorialIterative }