JavaScript Program to Calculate nPr (Permutations)
Last Updated :
04 Oct, 2023
In this article, we are going to learn how to calculate permutations (nPr) by using JavaScript. Calculating nPr, or permutations means finding the number of unique ordered arrangements of r items from a set of n distinct items. Where order matters and no item is repeated.
The formula for calculating permutations is n factorial divided by (n-r) factorial where n and r are integers and n is greater than or equal to r. The mathematical representation is as follows:
P(n, r) = n! / (n - r)!
For n ≥ r ≥ 0
There are several methods that can be used to Calculating nPr (Permutations) by using javascript, which are listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
In this approach, we will first calculate the factorial of the given number using recursion and then create a function named "permutation" to calculate and return the division of n factorial with (n-r) factorial.
function factorial(n) {
if (n <= 1) return 1
return n * factorial(n - 1)
}
function permutation(n, r) {
if (n < r) return -1
return factorial(n) / factorial(n - r)
};
Example: In this example, factorial to find the factorial of a number and permutation to calculate nPr (permutations). and permutation function to calculate permutations, denoted as "nPr," where "n" represents the total number of items, and "r" represents the number of items selected at a time.
JavaScript
function factorial(n) {
if (n <= 1) return 1
return n * factorial(n - 1)
}
function permutation(n, r) {
if (n < r) return -1
return factorial(n) / factorial(n - r)
}
console.log("4 P 1: ", permutation(4, 1))
console.log("13 P 2: ", permutation(13, 2))
Output4 P 1: 4
13 P 2: 156
Approach 2: Using for Loop
In this approach, permutation function using a loop to calculate nPr. It starts with n, and in each iteration, multiplies by decreasing values until r elements are chosen.
We know that:
n! = n*(n-1)*(n-2)*.....2*1
(n-r)! = (n-r)*(n-r-1)*(n-r-2)*....2*1
We also know that r is less than or equal to n and hence, if we divide the above two equations we get:
n! / (n-r)! = n*(n-1)*(n-2)*.....*(n - (r-2))*(n - (r-1))
Hence, in this approach we initialize a variable named "result" with value n and then using a loop from 1 to (r-1) we calculate the difference with "n" and then multiply to the result.
function permutation(n, r) {
if (n < r) return -1
let result = n
for (let i = 1; i < r; i++) result *= n - i;
return result
};
Example: In this example we are using the above-explained approach.
JavaScript
function permutation(n, r) {
if (n < r) return -1
let result = n
for (let i = 1; i < r; i++)
result *= n - i;
return result
}
console.log("4 P 1: ", permutation(4, 1))
console.log("13 P 2: ", permutation(13, 2))
Output4 P 1: 4
13 P 2: 156
Similar Reads
JavaScript Program to Find Lexicographically Next Permutation Given an array of distinct integers, we want to find the lexicographically next permutation of those elements. In simple words, when all the possible permutations of an array are placed in a sorted order, it is the lexicographic order of those permutations. In this case, we have to find the next gre
5 min read
Permutations in which n People can Occupy r Seats in a Classroom using JavaScript In this article, we'll explore how to calculate permutations in JavaScript, specifically focusing on scenarios where 'n' people need to occupy 'r' seats in a classroom. Permutations, which determine the number of ways people can be arranged, are essential in various settings like event planning. Exa
3 min read
JavaScript Program to Print all Possible Strings that can be Made by Placing Spaces In this article, We are going to see how can we print all possible strings that can be made by placing spaces. We will add spaces in between the strings and print every possible value. Example: Input: "ABCD"Output: 'A B C D' 'A B CD' 'A BC D' 'A BCD' 'AB C D' 'AB CD' 'ABC D' 'ABCD'Table of Content B
5 min read
PHP Program to Calculate the Permutation nPr This article will show you how to calculate the Permutation nPr using PHP. Permutations refer to the different ways in which a set of items can be arranged. In mathematics, the number of permutations of 'n' distinct items taken 'r' at a time is denoted as nPr. Calculating nPr, or permutations means
2 min read
Generate a Random Permutation of 1 to n in JavaScript In programming, creating a random permutation of numbers from 1 to n is a common task. A random permutation means randomly rearranging the elements that make up a permutation. Below are the methods to generate a random permutation of 1 to n in JavaScript: Table of Content Fisher-Yates (Knuth) Algori
2 min read
Print all Permutation of Array using JavaScript Permutations of Array are a fundamental concept in combinatorics and algorithms, showcasing all possible arrangements of its elements. Examples: Input: nums = [1, 2, 3] Output: [ [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2] ] Explanation: There are 6 possible permutations Input:
2 min read