Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add permutation calculation example #134

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions mathematics/permutation_calculation.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Permutation Calculation
calculate_permutations <- function(n, r) {

#' @description Calculates the number of permutations of n objects taken r at a time.
#' @param n Total number of objects
#' @param r Number of objects in each arrangement
#' @usage calculate_permutations(n, r)
#' @details Permutations represent the number of ways to arrange r objects from n.
#' It is calculated as n! / (n - r)! and is widely used in combinatorics.
#' @references https://en.wikipedia.org/wiki/Permutation

if (r > n) stop("r must be less than or equal to n")

factorial <- function(x) if (x == 0) 1 else prod(1:x)

return(factorial(n) / factorial(n - r))
}

# Example
print(calculate_permutations(5, 3)) # expected 60
print(calculate_permutations(10, 2)) # expected 90
Loading