- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Calculate the Value of nPr in Swift?
nPr is known as n permutation r, where n represents the total numbers and r represents the arrangement of the elements. A permutation is known as the arrangement of elements in a specified order. Elements can arrange either in a sequence or in a linear order, for example, we have a set of elements [2, 4], so the permutation is: [4, 2], [2, 4]. In permutation the order of elements matters whereas in combination the order of the elements does not matters.
We can calculate the value of nPr with the help of the following formula:
Formula
nPr = n!/(n - r)!
Example Demonstration
Input 1
n = 10 , r = 5
Output
Input
n = 8 , r = 4
Output
Here, we have n = 10 and r = 5 so the value nPr is 30240. Similarly, n = 8 and r = 4, so the value of nPr is 1680.
Algorithm
Step 1 − Create a function to find the factorial of the given number.
Step 2 − Create another function to calculate nPr according to the given formula.
Step 3 − Declare two variables to store the value of N and R.
Step 4 − Now call the nPr function and pass N and R into it.
Step 5 − Display the output.
Example
In the following Swift program, we calculate the value of nPr. So for that first we create a function named findFactorial() to calculate the factorial of the specified number. Then will create another function named calculateNPR() to find the value of the given nPr. In this function, we first check if the value of n and r are valid. If not, then return −1 which represents the value of n or r is not valid. If the value of n and r is valid, then we will find the value of nPr using the given formula and return the final value of nPr. Here to find the factorial we will use the findFactorial() function.
import Foundation import Glibc // Function to calculate the factorial func findFactorial(number: Int) -> Int { var fact = 1 for x in 1...number { fact *= x } return fact } // Function to calculate nPr func calculateNPR(n: Int, r: Int) -> Int { if n < 0 || r < 0 || r > n { return -1 } let numerator = findFactorial(number:n) let denominator = findFactorial(number: n - r) let nPr = numerator / denominator return nPr } // Test case let N = 10 let R = 5 let resultantNPR = calculateNPR(n: N, r:R) if resultantNPR != -1 { print("\(N)P\(R):", resultantNPR) } else { print("Please enter a valid input") }
Output
10P5: 30240
Example
In the following Swift program, we calculate the value of nPr. So for that first we create a function named findFactorial() to calculate the factorial of the specified number. Then will create another function named calculateNPR() to find the value of the given nPr. In this function, we will find the value of nPr using the given formula and return the final value of nPr. Here to find the factorial of the given number we will use the findFactorial() function.
import Foundation import Glibc // Function to calculate the factorial func findFactorial(number: Int) -> Int { if (number <= 1){ return 1 } return number * findFactorial(number: number - 1) } // Function to calculate nPr func calculateNPR(n: Int, r: Int) -> Int { let nPr = findFactorial(number: n)/findFactorial(number: n-r) return nPr } // Test case let N = 6 let R = 3 let resultantNPR = calculateNPR(n: N, r:R) print("\(N)P\(R):", resultantNPR)
Output
6P3: 120
Real−life usage of nPr
The real−life usage of the nPr value is:
You can use nPr to calculate a secure password.
Using nPr you can do seating arrangements.
Using nPr you can find the possible combinations in a game or lottery.
It can also be used in creating secure encryption algorithms.
Conclusion
So this is how we can calculate the value of nPr. nPr is commonly used for counting arrangments, probability, combinational analysis, permutation with repetitions, etc. It is a way of arrangments of numbers or elements. Hence you can use any of the above methods to calculate the value of nPr.