Learn

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:

26×25×24×23×22=7,893,60026 \times 25 \times 24 \times 23 \times 22 = 7,893,600

different possible passwords! A computer can easily brute force this password in a short amount of time.

The general formula for this is:

P=n(n1)(n2)(n3)(n4)P = n(n-1)(n-2)(n-3)(n-4)

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:

P(n,r)=n!(nr)!P(n,r) = \frac{n!}{(n-r)!}

Note the identity:

P(n,n)=n!(nn)!=n!0!=n!P(n,n) = \frac{n!}{(n-n)!} =\frac{n!}{0!} = n!

Instructions

1.

Calculate the number of ways to list the 50 states of The United States of America and store it in the variable called fifty_states.

2.

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 seven_states.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?