Horje
JavaScript Program to Calculate nPr (Permutations)

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.

Approach 1: Using Recursion

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))

Output

4 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))

Output

4 P 1:  4
13 P 2:  156



Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
How to Declare Two Dimensional Empty Array in JavaScript ? How to Declare Two Dimensional Empty Array in JavaScript ?
Language Translator using React Language Translator using React
Complete Guide for Redis Benchmark Complete Guide for Redis Benchmark
Complete Guide to Redis PHP Complete Guide to Redis PHP
Which Scalability approach is right for our Application? | System Design Which Scalability approach is right for our Application? | System Design

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9