PHP Program to Calculate the Permutation nPr Last Updated : 27 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 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 ≥ 0Factorial MethodThe formula for permutations is nPr = n! / (n-r)!, where '!' denotes the factorial of a number. The factorial of a number is the product of all positive integers up to that number. Example: PHP <?php // Function to calculate factorial function factorial($n) { if ($n == 0 || $n == 1) { return 1; } else { return $n * factorial($n - 1); } } // Function to calculate permutations function permutation($n, $r) { if ($n < $r) { return "Invalid input"; } return factorial($n) / factorial($n - $r); } // Driver code $n = 5; $r = 2; echo "Permutation: " . permutation($n, $r); ?> OutputPermutation: 20Iterative MethodThis approach calculates permutations using an iterative loop. Example: PHP <?php function permutation($n, $r) { if ($n < $r) { return "Invalid input"; } $result = 1; for ($i = 0; $i < $r; $i++) { $result *= ($n - $i); } return $result; } // Driver code $n = 5; $r = 2; echo "Permutation: " . permutation($n, $r); ?> OutputPermutation: 20 Comment More infoAdvertise with us Next Article PHP Program for Program to cyclically rotate an array by one B blalverma92 Follow Improve Article Tags : PHP Geeks Premier League 2023 Similar Reads PHP program to find the Standard Deviation of an array Given an array of elements. We need to find the Standard Deviation of the elements of the array in PHP. Examples: Input : array(2, 3, 5, 6, 7)Output : 1.5620499351813Input : array(1, 2, 3, 4, 5)Output : 1 The following problem can be solved using the PHP inbuilt functions. The inbuilt functions used 2 min read PHP Program for Program to cyclically rotate an array by one Write a PHP program for a given array, the task is to cyclically rotate the array clockwise by one time. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: arr[] = {5, 1, 2, 3, 4} Input: arr[] = {2, 3, 4, 5, 1}Output: {1, 2, 3, 4, 5} Recommended: Please solve it on âPRACTICEâ first, before moving on to 5 min read PHP DsSequence Functions Complete Reference A Sequence is used to arrange the values in a single and linear dimension way. In some languages, the sequence refers to the List. The sequence is similar to the array which is used as incremental integer keys are listed below: The index value of the sequence are [0, 1, 2, â¦, size - 1], where size r 3 min read PHP array_rand() Function This inbuilt function of PHP is used to fetch a random number of elements from an array. The element is a key and can return one or more than one key. On a practical basis, this is not that useful because the function uses pseudo-random number generator that is not suitable for cryptographic purpose 2 min read PHP array_count_values() Function The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi 2 min read Different Ways to Generate Permutations of an Array Permutations are like the magic wand of combinatorics, allowing us to explore the countless ways elements can be rearranged within an array. Whether you're a coder, a math enthusiast, or someone on a quest to solve a complex problem, understanding how to generate all permutations of an array is a va 6 min read Like