From e2a92fdf2ad5eaa515b5e9095cdaf715bc34a8c2 Mon Sep 17 00:00:00 2001 From: Shreyas-OwO Date: Sat, 17 Oct 2020 02:09:08 +0530 Subject: [PATCH 1/7] calculates the number of permutations and combinations --- Maths/PermutationAndCombination.js | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Maths/PermutationAndCombination.js diff --git a/Maths/PermutationAndCombination.js b/Maths/PermutationAndCombination.js new file mode 100644 index 0000000000..4bd4c06fc3 --- /dev/null +++ b/Maths/PermutationAndCombination.js @@ -0,0 +1,68 @@ +/** + * @details Calculates the number of permutations and combinations. + */ + +/** + * @brief Calculates the factorial of the given number. + * @param num: integer + * @details Factorial of n = n * (n - 1) * (n - 2) * ... * 1 + * @returns integer: Factorial of the number. + NaN: if negative number if provided. + */ +const factorial = (n) => { + if(n >= 0) { + if(n == 0) { + return 1; + } + else { + return n * factorial(n - 1); + } + } + else { + return NaN; + } +} + +/** + * @brief Calculates the number of Permutations from the given data. + * @param + * n: integer -> number of items. + * r: integer -> number of times n is taken. + * @returns integer: The number of permutations. + NaN: if negative number is provided. + */ +const permutation = (n, r) => { + return factorial(n) / factorial(n - r); +} + +/** + * @brief Calculates the number of Combinations from the given data. + * @param + * n -> number of items. + * r -> number of times n is taken. + * @returns integer: The number of combinations. + NaN: if negative number is provided. + */ +const combination = (n, r) => { + return factorial(n) / (factorial(r) * factorial(n - r)); +} + +// Exports the functions to be used in other files. +module.exports.factorial = factorial; +module.exports.permutation = permutation; +module.exports.combination = combination; + +/** + * @example + + const funcs = require("./PermutationAndCombination.js"); + + console.log(funcs.factorial(5)); + console.log(funcs.permutation(5, 2)); + console.log(funcs.combination(5, 2)); + + * @output + 120 + 20 + 10 + */ From 4a383079c3993c2dd806efc74e8c985367f5d39b Mon Sep 17 00:00:00 2001 From: Shreyas-OwO Date: Sat, 17 Oct 2020 02:09:44 +0530 Subject: [PATCH 2/7] calculates the number of permutations and combinations --- Maths/PermutationAndCombination.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/PermutationAndCombination.js b/Maths/PermutationAndCombination.js index 4bd4c06fc3..c35f5ce2bd 100644 --- a/Maths/PermutationAndCombination.js +++ b/Maths/PermutationAndCombination.js @@ -63,6 +63,6 @@ module.exports.combination = combination; * @output 120 - 20 - 10 + 20 + 10 */ From 6f5a026eb41da4e69460a5f86cf682f216141ed1 Mon Sep 17 00:00:00 2001 From: Shreyas-OwO Date: Sat, 17 Oct 2020 02:16:59 +0530 Subject: [PATCH 3/7] calculates the number of permutations and combinations --- Maths/PermutationAndCombination.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Maths/PermutationAndCombination.js b/Maths/PermutationAndCombination.js index c35f5ce2bd..d9f54fd3bc 100644 --- a/Maths/PermutationAndCombination.js +++ b/Maths/PermutationAndCombination.js @@ -1,5 +1,6 @@ /** * @details Calculates the number of permutations and combinations. + * @external_link (Permutation And Combinations)[https://www.geeksforgeeks.org/permutation-and-combination/] */ /** From 944e41be8cadfe50ab1e096e7eb5550d84532338 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 16 Oct 2020 20:47:41 +0000 Subject: [PATCH 4/7] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 61078db7e4..0a28c0e884 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -112,6 +112,7 @@ * [PerfectCube](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PerfectCube.js) * [PerfectNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PerfectNumber.js) * [PerfectSquare](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PerfectSquare.js) + * [PermutationAndCombination](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PermutationAndCombination.js) * [PiApproximationMonteCarlo](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PiApproximationMonteCarlo.js) * [Polynomial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Polynomial.js) * [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeCheck.js) From 64a2c5ba0f714497a4be7d884e529674e91df5a6 Mon Sep 17 00:00:00 2001 From: Shreyas-OwO Date: Sat, 17 Oct 2020 02:31:20 +0530 Subject: [PATCH 5/7] calculates the number of permutations and combinations --- Maths/PermutationAndCombination.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Maths/PermutationAndCombination.js b/Maths/PermutationAndCombination.js index d9f54fd3bc..4672fe8acf 100644 --- a/Maths/PermutationAndCombination.js +++ b/Maths/PermutationAndCombination.js @@ -11,17 +11,15 @@ NaN: if negative number if provided. */ const factorial = (n) => { - if(n >= 0) { - if(n == 0) { - return 1; - } - else { - return n * factorial(n - 1); - } - } - else { - return NaN; - } + if (n >= 0) { + if (n == 0) { + return 1 + } else { + return n * factorial(n - 1) + } + } else { + return NaN + } } /** @@ -33,7 +31,7 @@ const factorial = (n) => { NaN: if negative number is provided. */ const permutation = (n, r) => { - return factorial(n) / factorial(n - r); + return factorial(n) / factorial(n - r) } /** @@ -45,13 +43,13 @@ const permutation = (n, r) => { NaN: if negative number is provided. */ const combination = (n, r) => { - return factorial(n) / (factorial(r) * factorial(n - r)); + return factorial(n) / (factorial(r) * factorial(n - r)) } // Exports the functions to be used in other files. -module.exports.factorial = factorial; -module.exports.permutation = permutation; -module.exports.combination = combination; +module.exports.factorial = factorial +module.exports.permutation = permutation +module.exports.combination = combination /** * @example From 15014ff7561ace3fa10137719c633eff2adb249c Mon Sep 17 00:00:00 2001 From: Shreyas-OwO Date: Sat, 17 Oct 2020 02:33:47 +0530 Subject: [PATCH 6/7] calculates the number of permutations and combinations --- Maths/PermutationAndCombination.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/PermutationAndCombination.js b/Maths/PermutationAndCombination.js index 4672fe8acf..4059abd8ed 100644 --- a/Maths/PermutationAndCombination.js +++ b/Maths/PermutationAndCombination.js @@ -12,7 +12,7 @@ */ const factorial = (n) => { if (n >= 0) { - if (n == 0) { + if (n === 0) { return 1 } else { return n * factorial(n - 1) From f87fc81d0514e1cb0ea6276f385aa6cdfbc71b54 Mon Sep 17 00:00:00 2001 From: Shreyas-OwO Date: Sat, 17 Oct 2020 04:58:19 +0530 Subject: [PATCH 7/7] fixed a typo --- Maths/PermutationAndCombination.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/PermutationAndCombination.js b/Maths/PermutationAndCombination.js index 4059abd8ed..1146230fb7 100644 --- a/Maths/PermutationAndCombination.js +++ b/Maths/PermutationAndCombination.js @@ -8,7 +8,7 @@ * @param num: integer * @details Factorial of n = n * (n - 1) * (n - 2) * ... * 1 * @returns integer: Factorial of the number. - NaN: if negative number if provided. + NaN: if negative number is provided. */ const factorial = (n) => { if (n >= 0) {