Skip to content

Commit acbb8d8

Browse files
Add permutation calculation example (#134)
1 parent 92324e2 commit acbb8d8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

mathematics/permutation_calculation.r

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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

0 commit comments

Comments
 (0)