In the previous exercise, you learned how to compute the number of permutations of n items. What if you were only interested in the permutation of r out of those n items (permutation of four out of seven items for example)? Let’s see how we can do this.
Consider this problem: a website with weak password requirements requires that its users produce a password of five distinct, lowercase, English letters. How many possible permutations of passwords exist?
The English alphabet has 26 letters in it and your password only requires five distinct characters (which will produce weak passwords), therefore you have:
different possible passwords! A computer can easily brute force this password in a short amount of time.
The general formula for this is:
However, this is only valid for five options!
In the general case, if we have n! total permutations but we only want r options, we need to eliminate the remaining n-r possible options. We do this by dividing n! by (n-r)! leaving us with the r terms we want.
The formula for selecting (or arranging) r distinct objects from a set of n possible objects where their order matters and repetition is not allowed is:
Note the identity:
Calculate the number of ways to list the 50 states of The United States of America and store it in the variable called
Suppose we want to list only seven of these 50 states, how many different lists are possible? Modify your computation to eliminate the remaining 43 states and store that in the variable called