Counting Pizza Toppings* The commercial’s deal was: • 2 pizzas • up to 5 toppings on each • 11 toppings to choose from • all for $7.98 (back in 1997). The commercial’s math kid claimed there are 1,048,576 possibilities. Let’s do the calculation ourselves. Q. How many ways can we order a pizza with 0 toppings? A. 1 Q. How many ways can we order a pizza with 1 topping? A. 11
*http://mindyourdecisions.com/blog/2011/04/27/math-problem-pizza-topping-combinations/ 1
Q. How many ways can we order a pizza with 2 toppings? We have 11 choices for the first topping; A. 10 choices for the second topping 11*10 =110
Q. How many ways can we order a pizza with 3 toppings? A. • 11*10*9 = 990 • Does the order that the toppings are picked matter? How many times have we over counted? 6 = 3*2*1 = 3! (A,B,C) (A,C,B) (B,A,C) (B,C,A) (C,A,B) (C,B,A) These 6 choices are actually the same choice Q. How many ways can we order a pizza with 4 toppings? A. 11*10*9*8/4! Q. How many ways can we order a pizza with 5 toppings? A. 11*10*9*8*7/5!
2
Therefore the total number of ways to order a pizza with up to 5 toppings when choosing from 11 toppings is: 1 + 11 + 11*10/2 + 11*10*9/3! + 11*10*9*8/4! + 11*10*9*8*7/5! = 1 + 11 + 55 + 65 + 330 + 462 = 1024
Q. How did they get 1,048,576 in the commercial? 1024 ways to get the first pizza; 1024 ways to get the second pizza
A.
1024 * 1024 = 1048576
Q. Is 1,048,576 the correct answer? A. No. Q. What is their mistake? and how do we correct for it? Order doesn't matter for the pizzas (Pizza A,Pizza B) is the same choice as (Pizza B, Pizza A) A. So divided by 2 1048576 / 2 = 524288 Q. This is stilll not quite correct. Why? We didn't consider when we have 2 of the same pizzas A. and there are 1024 ways to order two identical pizzas with no need to divide by 2 So we need to add 1024/2 back 524288 + 512 = 524800 This should be the final answer
3
Q. How do we know that this is the correct answer? A. One way to convince ourselves is try to find another way to count the same problem. We counted all possible orders and then removed duplicates. Exercise. Recount using a different method. The number of ways to get 2 different pizzas is 1024*1023 / 2 = 523,776 And we have 1024 ways to get two identical pizzas 523,776 + 1024 = 528,800 Note: In our first way of counting, we are doing: The number of all possible ways to get two pizza: 1024*1024 = 1,048,576 Remove the dupicates ((A,B) and (B,A) should be counted as one choice) : 1,048,576 / 2 = 524,288 However, there are 1024 ways of ordering two identical pizzas which don't need to be divided by 2; we have to add this back: 524,288 + 1024 / 2 = 524,800 Not understanding why we have to remove and readd? Find an easier example: for each pizza suppose there are only 3 choices A, B, C so geting two pizza 3 * 3 is (A, A) (A, B) (A, C) (B, A) (B, B), (B, C) (C, A) (C, B) (C, C) Remove the dupicates 3 * 3 / 2 (A, A)/2 (B, B)/2 (C, C)/2 (A, B) (A, C) (B, C) You can see from here that (A, A) (B, B) (C, C) don't need to be divided by 2 So add them back 3 * 3 / 2 + 3
Exercise. How many ways are there to order the pizzas, if double toppings are allowed (ie., two of the toppings are the same)? 4
Example. Creating a secure password.
To create a account, you are required to give 8 or more alphanumeric characters. Q. Why so many? Why not just 4? Your bank password only has 4 characters
A. Let’s count how many possible passwords there are of lengths 4 and 8 and compare.
5
Q. How many alphanumeric characters are there? A.
a-z, A-Z, 0-9 26 + 26 + 10 = 62
Q. How many passwords are there of length 4? (The order counts)
A. 62 * 62 * 62 * 62 = 62^4 > 1.4 * 10^7 Q. Why do we multiply instead of add? A. Because for each position, we have 62 choices: we have 62 choices for position 1, 62 choices for position 2 and so on
Q. How many passwords are there of length 8? A. 62^8 > 2.1 * 10^14 So there are more than 14 million times as many passwords of length 8 than of length 4.
6
The Sum Rule When counting we need to be able to determine whether to sum or multiply the number of objects. The Sum Rule. If an operation can be performed in n different ways, each having xi possible outcomes, then the total number of outcomes possible is n X xi i=1
Example. Ordering pizza. Suppose a pizza shop offers 5 types of toppings, and one has the choice of a 3 topping, 2 topping or 1 topping pizza. Determine how many different pizzas can be ordered. Number of Toppings
Number of Pizza Choices (xi ) 5
1 2 3
5 * 4 / 2 = 10 5 * 4 * 3 / 3! = 10
Total number of ways to order a pizza: 3 X i=1
xi = 25
Why 3 topping has 10 pizza choices? (5 choices for the first topping, 4 choices for the second topping, 3 choices for the third topping (take away one choice each time to avoid getting the same topping) (order does not matter) so divided by 3!) 7
The Product Rule The Product Rule. Suppose an operation takes k steps and that • the first step can be performed in x1 ways • the second step can be performed in x2 ways • ... • the kth step can be performed in xk ways then the whole operation can be performed in k Y
xi = x1 · x2 · · · xk−1 · xk
i=1
ways. Example. Given 4 cities A, B, C and D. Suppose that there are • 5 routes between A and B, • 3 routes between B and C and • 4 routes between C and D. How many different routes are there from A to D? Number of AD routes = the order here matters!
A -> B -> C -> D: 5 * 3 * 4 = 60 A -> C -> B -> D: 5 * 4 * 3 = 60 so total 120
->
8
Q. We have seen two different counting scenarios - creating a Google password and ordering a pair of pizzas. Which one involved the Sum Rule and which one involved the Product Rule? Product Rule: both
A. Sum Rule: Pizza
Arrangements An arrangement is a grouping of objects. There are two types of arrangements: Definition. A permutation is an arrangement in which order matters. Definition. A combination is an arrangement in which order does not matter.
Permutations Definition An r-permutation of n distinct objects is an ordered arrangement of r of the n objects. We use the notation P (n, r). We can derive the formula as follows:
P (n , r ) = | n
n-1
n- 2
n-3
{z r
...
n-r+1
}
when you want to get r objects from n objects and order matters there are P(n, r) different ways to pick the r objects 9
Q. In terms of factorials, how can we rewrite this formula? n! / (n - r)! = n * (n - 1) * (n - 2)......(n - r + 1)....(1) / (n - r)! A. (here n-r+1 = 1)
Given n objects, how many ways can we rearrange or permute them?
P (n , n ) = n(n-1)(n-2)........(n-n+1) = n!
Combinations Definition. An r-combination of n distinct objects is an unordered selection, or subset of r of the n objects. We can think of combinations in terms of permutations. Q. Given P (n, r), the number of r-permutations of n objects, how can we derive the number C(n, r) of r-combinations of n objects? C(n,r) = n(n - 1)(n - 2)......(n - r + 1) / r! A. (Choose r objects from n where order does not matter) The relationship between P(n, r) and C(n, r) Because P(n, r) = n(n-1)...(n-r+1) So C(n, r) * P(r, r) = P(n, r) So C(n, r) = P(n, r) / P(r, r) = n! / [(n-r)! * r!]
10
Example. In Poker each player has 5 cards. A standard deck has 52 cards. How many 5-card hands are possible? Order does not matter C(52, 5) = 52! / (47! * 5!) = 2,598,960
A flush is when all 5 cards are of the same suit. If there are 4 suits, i.e., 13 cards per suit, how many ways are there to obtain a flush? Four ways to get a flush 1. 5 hearts C1(13, 5) 2. 5 diamonds C2(13, 5) 3. 5 spades C3(13, 5) 4 5 clubs C4(13, 5) They don't affect each others, so add them up Total = C1(13, 5) + C2(13, 5) + C3(13, 5) + C4(13, 5) = 4 * 13! / (5! * 8!) = 4 * 1287 = 5148
Q. Should flushes happen very often? No, 5148 / 2,598,960 = 2% It a very small possibility to get a flush
Exercise. How many different 8-digit binary sequences are there with six 1s and two 0s? We have six 1s and two 0s to put there are 8 positions If we places the 0s, that defines the 1s 8 choices to put my first 0 7 choices to put my second 0 Since the first 0 is actually the same as the second 0 So the order does not matter so it should be 8 * 7 / 2 = 28 or use the combination formula: C(8, 2) = 8! / (6! * 2!) = 28
11
Exercise. Given n non-parallel lines such that no three intersect in a point, determine how many triangles are formed.
e
f a
d b c
Triangles 4acf, 4bcd, 4def, 4abe are formed by four lines. Q. How many triangles are formed by 3 lines? A.
1
Q. How many triangles are formed by 4 lines? A. 4 Q. How can we phrase this question in terms of “choose”? A. for n lines, C(n, 3) Q. How would you prove your answer? A.
Induction! 12