8000 feat: Added Double Factorial Iterative Implementation by SpiderMath · Pull Request #187 · TheAlgorithms/TypeScript · GitHub
[go: up one dir, main page]

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 all 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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions maths/double_factorial_iterative.ts
Original file line number Diff line number Diff line change
@@ -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 }
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)
})
})
0