Skip to content

feat: Added Double Factorial Iterative Implementation #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
## Bit Manipulation
* [Is Power Of 2](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_2.ts)
* [Is Power Of 4](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_4.ts)
* [Log Two](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/log_two.ts)
* Test
* [Is Power Of 2.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_2.test.ts)
* [Is Power Of 4.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_4.test.ts)
* [Log Two.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/log_two.test.ts)

## Ciphers
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts)
Expand Down Expand Up @@ -71,6 +73,7 @@

## Dynamic Programming
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
* [Lcs](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/lcs.ts)

## Graph
* [Bellman Ford](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bellman_ford.ts)
Expand All @@ -97,6 +100,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)
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factorial.ts)
* [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts)
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts)
Expand Down Expand Up @@ -154,3 +158,4 @@
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_sort.ts)
* [Selection Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/selection_sort.ts)
* [Shell Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/shell_sort.ts)
* [Swap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/swap_sort.ts)
30 changes: 30 additions & 0 deletions maths/double_factorial_iterative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @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")
if(n === 0) return 1;
let doubleFactorial = 1;

for(let i = n; i > 0; i -= 2)
doubleFactorial *= i;

return doubleFactorial;
}

export { DoubleFactorialIterative };
7 changes: 7 additions & 0 deletions maths/test/double_factorial_iterative.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})