File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Permutation Calculation
2
+ calculate_permutations <- function (n , r ) {
3
+
4
+ # ' @description Calculates the number of permutations of n objects taken r at a time.
5
+ # ' @param n Total number of objects
6
+ # ' @param r Number of objects in each arrangement
7
+ # ' @usage calculate_permutations(n, r)
8
+ # ' @details Permutations represent the number of ways to arrange r objects from n.
9
+ # ' It is calculated as n! / (n - r)! and is widely used in combinatorics.
10
+ # ' @references https://en.wikipedia.org/wiki/Permutation
11
+
12
+ if (r > n ) stop(" r must be less than or equal to n" )
13
+
14
+ factorial <- function (x ) if (x == 0 ) 1 else prod(1 : x )
15
+
16
+ return (factorial(n ) / factorial(n - r ))
17
+ }
18
+
19
+ # Example
20
+ print(calculate_permutations(5 , 3 )) # expected 60
21
+ print(calculate_permutations(10 , 2 )) # expected 90
You can’t perform that action at this time.
0 commit comments