Topics
Permutations:
- Definition: Arrangements where the order matters
- Formula: The number of ways to arrange r items out of n is given by
Combinations:
- Definition: Selections of objects where order doesn’t matter
- Formula: Number of ways to choose r objects from a set of n distinct object . Also called as binomial coefficient
import math
def permutations(n, r):
return math.factorial(n) // math.factorial(n - r)
def combinations(n, r):
return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
Number of ways to reach
(m, n)
Starting at (0, 0), number of ways, if allowed to move only left and up will be:
C(m+n,m)
. This is because every path you take will consist of exactlym
moves to the right (R) andn
moves up (U). The total number of moves will always bem + n
.The problem boils down to figuring out how many ways you can arrange these
m
(R) moves andn
(U) moves, i.e. choosem
out of the totalm+n
moves to place the (R) moves, the remainingn
positions must be the (U) moves.