Skip to content

Commit c5b12db

Browse files
SpiderMathactions-userappgurueu
authored
feat: Added Double Factorial Iterative Implementation (#187)
* Update DIRECTORY.md * 🚀feat: added double factorial (iterative) * Update DIRECTORY.md * removed redundant if check, fix formatting * Update DIRECTORY.md --------- Co-authored-by: autoprettier <[email protected]> Co-authored-by: Lars Müller <[email protected]>
1 parent 26ac72d commit c5b12db

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
* [Calculate Median](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/calculate_median.ts)
109109
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/degrees_to_radians.ts)
110110
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/digit_sum.ts)
111+
* [Double Factorial Iterative](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/double_factorial_iterative.ts)
111112
* [Euler Totient](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/euler_totient.ts)
112113
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factorial.ts)
113114
* [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts)
@@ -128,6 +129,7 @@
128129
* [Number Of Digits](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/number_of_digits.ts)
129130
* [Pascals Triangle](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pascals_triangle.ts)
130131
* [Perfect Cube](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_cube.ts)
132+
* [Perfect Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_number.ts)
131133
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts)
132134
* [Prime Factorization](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/prime_factorization.ts)
133135
* [Primes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/primes.ts)

maths/double_factorial_iterative.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @function DoubleFactorialIterative
3+
* @description Calculate the double factorial of a number (iterative implementation)
4+
* @summary In mathematics, double factorial of a number n is denoted by n!!.
5+
* It is not to be confused with (n!)!, which is the factorial function iterated twice.
6+
* The double factorial is the product of all positive integers upto n that have the same parity (odd or even)
7+
* as n.
8+
* Therefore,
9+
* 9!! = 9 . 7 . 5 . 3 . 1
10+
* 10!! = 10 . 8 . 6 . 4 . 2
11+
*
12+
* Please note that for factorials of even numbers, the series ends at 2.
13+
* @see [Wikipedia](https://en.wikipedia.org/wiki/Double_factorial)
14+
* @see [Mathworld](https://mathworld.wolfram.com/DoubleFactorial.html)
15+
* @see [GeeksForGeeks](https://www.geeksforgeeks.org/double-factorial/)
16+
* @example DoubleFactorialIterative(4) = 8
17+
* @example DoubleFactorialIterative(5) = 15
18+
*/
19+
const DoubleFactorialIterative = (n: number) => {
20+
if(n < 0) throw new RangeError("The number needs to be non-negative")
21+
let doubleFactorial = 1
22+
23+
for(let i = n; i > 0; i -= 2)
24+
doubleFactorial *= i
25+
26+
return doubleFactorial
27+
}
28+
29+
export { DoubleFactorialIterative }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { DoubleFactorialIterative } from "../double_factorial_iterative";
2+
3+
describe("Double Factorial", () => {
4+
test.each([[4, 8], [5, 15], [10, 3840]])("%i!! = %i", (n, expected) => {
5+
expect(DoubleFactorialIterative(n)).toBe(expected)
6+
})
7+
})

0 commit comments

Comments
 (0)