diff --git a/DIRECTORY.md b/DIRECTORY.md index 3e6e6418..185bae95 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -108,6 +108,7 @@ * [Calculate Median](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/calculate_median.ts) * [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/degrees_to_radians.ts) * [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/digit_sum.ts) + * [Double Factorial Iterative](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/double_factorial_iterative.ts) * [Euler Totient](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/euler_totient.ts) * [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factorial.ts) * [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts) @@ -128,6 +129,7 @@ * [Number Of Digits](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/number_of_digits.ts) * [Pascals Triangle](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pascals_triangle.ts) * [Perfect Cube](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_cube.ts) + * [Perfect Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_number.ts) * [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts) * [Prime Factorization](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/prime_factorization.ts) * [Primes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/primes.ts) diff --git a/maths/double_factorial_iterative.ts b/maths/double_factorial_iterative.ts new file mode 100644 index 00000000..6044eca0 --- /dev/null +++ b/maths/double_factorial_iterative.ts @@ -0,0 +1,29 @@ +/** + * @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 } diff --git a/maths/test/double_factorial_iterative.test.ts b/maths/test/double_factorial_iterative.test.ts new file mode 100644 index 00000000..3c221785 --- /dev/null +++ b/maths/test/double_factorial_iterative.test.ts @@ -0,0 +1,7 @@ +import { DoubleFactorialIterative } from "../double_factorial_iterative"; + +describe("Double Factorial", () => { + test.each([[4, 8], [5, 15], [10, 3840]])("%i!! = %i", (n, expected) => { + expect(DoubleFactorialIterative(n)).toBe(expected) + }) +}) \ No newline at end of file