This post is part pedagogical rant, part discussion of a beautiful technique in combinatorics, both motivated by a recent exchange with a high school student, about an interesting dice game that seems to be a common introductory exercise in probability:

There are 12 horses, numbered 1 through 12, each initially at position 0 on a track. Play consists of a series of turns: on each turn, the teacher rolls two 6-sided dice, where the sum of the dice indicates which horse to advance one position along the track. The first horse to reach position wins the race.

At first glance, this seems like a nice exercise. Students quickly realize, for example, that horse #1 is a definite loser– the sum of two dice will never equal 1– and that horse #7 is the best bet to win the race, with the largest probability (1/6) of advancing on any given turn.

But what if a student asks, as this particular student did, “Okay, I can see how to calculate the distribution of probabilities of each horse advancing in a *single turn*, but what about the probabilities of each horse *winning the race*, as a function of the race length ?” This makes me question whether this is indeed such a great exercise, at least as part of an *introduction* to probability. What started as a fun game and engaging discussion has very naturally led to a significantly more challenging problem, whose solution is arguably beyond most students– and possibly many teachers as well– at the high school level.

I like this game anyway, and I imagine that I would likely use it if I were in a similar position. Although the methods involved in an *exact* solution might be inappropriate at this level, the game still lends itself nicely to investigation via Monte Carlo simulation, especially for students with a programming bent.

**Poissonization**

There *is* an exact solution, however, via several different approaches. This problem is essentially a variant of the coupon collector’s problem in disguise: if each box of cereal contains one of 12 different types of coupons, then if I buy boxes of cereal until I have of one type of coupon, what is the probability of stopping with each type of coupon? Here the horses are the coupon types, and the dice rolls are the boxes of cereal.

As in the coupon collector’s problem, it is helpful to modify the model of the horse race in a way that, at first glance, seems like unnecessary *additional* complexity: suppose that the dice rolls occur at times distributed according to a Poisson process with rate 1. Then the advances of each individual horse (that is, the subsets of dice rolls with each corresponding total) are also Poisson processes, each with rate equal to the probability of the corresponding dice roll.

Most importantly, these individual processes are *independent*, meaning that we can easily compute the probability of desired states of the horses’ positions on the track at a particular time, as the *product* of the individual probabilities for each horse. Integrating over all time yields the desired probability that horse wins the race:

Intuitively, horse advances on the final dice roll, after exactly previous advances, while each of the other horses has advanced at most times.

**Generating functions**

This “Poissonization” trick is not the only way to solve the problem, and in fact may be less suitable for implementation without a sufficiently powerful computer algebra system. Generating functions may also be used to “encode” the possible outcomes of dice rolls leading to victory for a particular horse, as follows:

where the probability that horse wins on the -st dice roll is times the coefficient of in . Adding up these probabilities for all possible yields the overall probability of winning. This boils down to simple polynomial multiplication and addition, allowing relatively straightforward implementation in Python, for example.

The results are shown in the following figure. Each curve corresponds to a race length, from in black– where the outcome is determined by a single dice roll– to in purple.

As intuition might suggest, the longer the race, the more likely the favored horse #7 is to win. This is true for *any* non-uniformity in the single-turn probability distribution. For a contrasting example, consider a race with just 6 horses, with each turn decided by a *single* die roll. This race is fair no matter how long it is; every horse always has the same probability of winning. But if the die is loaded, no matter how slightly, then the longer the race, the more advantage to the favorite.

]]>

Once again motivated by a series of interesting posts by Mark Dominus, a “24 puzzle” is a set of 4 randomly selected numbers from 1 to 9, where the objective is to arrange the numbers in an arithmetic expression using only addition, subtraction, multiplication, and division, to yield the value 24. For example, given the numbers (3, 5, 5, 9), one solution is

Solutions are in general not unique; for example, another possibility is

This is a great game for kids, and it can be played with no more equipment than a standard deck of playing cards: remove the tens and face cards, shuffle the remaining 36 cards, and deal 4 cards to “generate” a puzzle. Or keep all 52 cards, and generate potentially more difficult puzzles involving numbers from 1 to 13 instead of 1 to 9.

Or you could play the game using a different “target” value other than 24… but *should* you? That is, is there anything special about the number 24 that makes it more suitable as a target value than, say, 25, or 10, etc.? And whatever target value we decide to use, what makes some puzzles (i.e., sets of numbers) more difficult to solve than others? What are the *hardest* puzzles? Finally, subtraction is one of the allowed *binary* operations; what about unary minus (i.e., negation)? Is this allowed? Does it matter? These are the sort of questions that make a simple children’s game a great source of interesting problems for both mathematics and computer science students.

(Aside: Is it “these are the *sort* of questions” or “these are the *sorts* of questions”? I got embarrassingly derailed working on that sentence. I could have re-worded to avoid the issue entirely, but it’s interesting enough that I choose to leave it in.)

**Enumerating possible expressions**

Following is my Mathematica implementation of a 24 puzzle “solver”:

trees[n_Integer] := trees[n] = If[n == 0, {N}, Flatten@Table[Outer[Star, trees[k], trees[n - 1 - k]], {k, 0, n - 1}]] sub[expr_, values_List, ops_List] := Quiet@Fold[ ReplacePart[#1, MapThread[Rule, {Position[#1, First[#2]], Last[#2]}]] &, expr, {{N, values}, {Star, ops}}] search[visit_, values_List, ops_List] := Outer[visit, trees[Length[values] - 1], Permutations[values], Tuples[ops, Length[values] - 1], 1]

The function `trees[n]`

enumerates all possible expression trees involving binary operations, which are counted by the Catalan numbers. Each expression tree is just a “template,” with placeholders for the numbers and operators that will be plugged in using the function `sub`

. For example, a standard 24 puzzle with 4 numbers requires operators, in one of the following 5 patterns:

N * (N * (N * N)) N * ((N * N) * N) (N * N) * (N * N) (N * (N * N)) * N ((N * N) * N) * N

The function `search`

takes a puzzle represented as a set of numbers and set of available operators, and simply explores the outer product of all possible expression trees, permutations of numbers, and selections of operators, “visiting” each resulting expression in turn.

The choice of visitor depends on the question we want to answer. For example, the following code solves a given puzzle for a given target value, with a visitor that checks each evaluated expression’s value against the target, and pretty-prints the expression if it matches:

show[expr_, values_List, ops_List] := ToExpression[ ToString@sub[expr, ToString /@ values, ToString /@ ops], InputForm, HoldForm] solve[target_Integer, values_List, ops_List] := Reap@search[ If[sub[##] == target, Sow@show[##]] &, values, ops] // Last // Flatten

But another useful visitor is just `sub`

itself, in which case `search`

computes the set of *all* possible values that can be made from all possible arithmetic arrangements of the given numbers and operators. We can use this information in the following sections.

**Why 24?**

Suppose that we draw 4 random cards from a deck of 36 cards (with face cards removed); what is the probability that the resulting puzzle is solvable? The answer depends on the target– are we trying to find an expression that evaluates to 24, or to some other value? The following figure shows the probability that a randomly selected puzzle is solvable, as a function of the target value.

The general downward trend makes sense: it’s more difficult to make larger numbers. But most interesting are the targets that are multiples of 12 (highlighted by the vertical grid lines), whose corresponding probabilities are distinctly higher than their neighbors. This also makes sense, at least in hindsight (although I doubt I would have predicted this behavior): multiples of 12 have a relatively large number of factors, allowing more possible ways to be “built.”

So this explains at least in part why 24 is “the” target value… but why not 12, for example, especially since it has an even higher probability of being solvable (i.e., an even *lower* probability of frustrating a child playing the game)? The problem is that the target of 12 seems to be *too easy*, as the following figure shows, indicating for each target the expected number of different solutions to a randomly selected solvable puzzle:

Of course, this just pushes the discussion in the other direction, asking whether a *larger* multiple of 12, like 36, for example, wouldn’t be an even better target value, allowing “difficult” puzzles while still having an approximately 84% probability of being solvable. And it arguably would be, at least for more advanced players or students.

More generally, the following figure shows these two metrics together, with the expected number of solutions on the *x*-axis, and the probability of solvability on the *y*-axis, for each target value, with a few highlighted alternative target values along/near the Pareto frontier:

**The hardest 24 puzzles**

Finally, which 24 puzzles are the hardest to solve? The answer depends on the metric for difficulty, but one reasonable choice is the number of distinct solutions. That is, among all possible expression trees, permutations of the numbers in the puzzle, and choices of available operators, how many yield the desired target value of 24? The fewer the possible arrangements that work, the more difficult the puzzle.

It turns out that there are relatively few puzzles that have a *unique* solution, with exactly one possible arrangement of numbers and operators that evaluates to 24. The list is below, where for completeness I have included all puzzles involving numbers up to 13 instead of just single digits. (It’s worth noting that Mark’s example— which is indeed difficult– of arranging (2, 5, 6, 6) to yield 17, would *not* make this list. And some of the puzzles that *are* on this list are arguably pretty easy, suggesting that there is something more to “hardness” than *just* uniqueness.)

- (1, 2, 7, 7)
- (1, 3, 4, 6)
- (1, 5, 11, 11)
- (1, 6, 6, 8)
- (1, 7, 13, 13)
- (1, 8, 12, 12)
- (2, 3, 5, 12)
- (3, 3, 5, 5)
- (3, 3, 8, 8)
- (4, 4, 10, 10)
- (5, 5, 5, 5)
- (5, 5, 8, 8)
- (5, 5, 9, 9)
- (5, 5, 10, 10)
- (5, 5, 11, 11)
- (5, 5, 13, 13)

And one more: (3, 4, 9, 10), although this one is special. It has no solution involving only addition, subtraction, multiplication, and division. For this puzzle, we must expand the set of available operators to also include exponentiation… and then the solution is unique.

]]>

This was a fun exercise, motivated by several interesting recent posts by Mark Dominus at The Universe of Discourse about finding anagrams of individual English words, such as (*relationships*, *rhinoplasties*), and how to compute a “score” for such anagrams by some reasonable measure of the complexity of the rearrangement, so that (*attentiveness*, *tentativeness*), with a common 8-letter suffix, may be viewed as less “interesting” than, say, the more thoroughly shuffled (*microclimates*, *commercialist*).

The proposed scoring metric is the size of a “minimum common string partition” (MCSP): what is the fewest number of blocks of consecutive letters in a partition of the first word that may be permuted and re-concatenated to yield the second word? For example, the above word *attentiveness* may be partitioned into 3 blocks, *at*+*tent*+*iveness*, and transposing the first two blocks yields *tent*+*at*+*iveness*. Thus, the score for this anagram is only 3. Compare this with the score of 12 for (*intolerances*, *crenelations*), where all 12 letters must be rearranged.

**Computing MCSP**

I wanted to experiment with this idea in a couple of different ways. First, as Mark points out, the task of *finding* the anagrams themselves is pretty straightforward, but computing the resulting MCSP scores is NP-complete. Fortunately, there is a nice characterization of the solution– essentially the same “brute force” approach described by Mark– that allows concise and reasonably efficient implementation.

Consider an anagram of two words with letters each, where the necessary rearrangement of letters of to produce is specified by a permutation

where the -th letter in is the -th letter in . This permutation of *individual* letters corresponds to a permutation of *blocks of consecutive* letters, where the number of such blocks– the MCSP score– is

Computing an MCSP is hard because this permutation transforming into is not necessarily unique; we need the permutation that minimizes . The key observation is that each candidate permutation may be decomposed into , where transforms any canonical (e.g., sorted) ordering of letters into . So we can fix, say, , and the enumeration of possible is easy to express, since we are using the *sorted* list of letters as our starting point.

The following Mathematica function implements this approach:

anagramScore[w1_String, w2_String] := Module[ {s1 = Characters[w1], s2 = Characters[w2], p1, p2, i}, p1 = Ordering@Ordering[s1]; p2 = Ordering@Ordering[s2]; Length[s1] - Max@Outer[ Count[ Differences[Ordering[ReplacePart[p1, {##} // Flatten]][[p2]]], 1] &, Sequence @@ Map[ (i = Position[s1, #] // Flatten; Thread[i -> #] & /@ Permutations[p1[[i]]]) &, Union[s1] ], 1]]

Using this, we find, as Mark does, that an anagram with maximum MCSP score of 14 is (*cinematographer*, *megachiropteran*)… along with the almost-as-interesting (*involuntariness*, *nonuniversalist*), but also other fun ones farther down the list, such as (*enthusiastic*, *unchastities*) with a score of 9.

**Scoring Anagrams Using MCSP and Frequency**

From Mark’s post:

Clearly my chunk score is not the end of the story, because “notaries / senorita” should score better than “abets / baste” (which is boring) or “Acephali / Phacelia” (whatever those are), also 5-pointers. The length of the words should be worth something,

and the familiarity of the words should be worth even more[my emphasis].

The problem is that an MCSP score alone is a pretty coarse metric, since it’s an integer bounded by the length of the words in the dictionary. So the second idea was to refine the ordering of the list of anagrams as Mark suggests, with a lexicographic sort first by MCSP score, then by (average) frequency of occurrence in language, as estimated using the Google Books Ngrams data set (methodology described in more detail here). The expectation was that this would make browsing a long list easier, with more “recognizable” anagrams appearing together near the beginning of each MCSP grouping.

However, because I wanted to try to reproduce Mark’s results, I also needed a larger dictionary that contained, for example, *megachiropteran*. (which, by the way, is a bat that can have a wing span of over 5 feet). I used the American English version of the Spell Checker Oriented Word List (SCOWL), combined with the Scrabble and ENABLE2k word lists used in similar previous experiments– which, interestingly, *alone* contain many anagrams *not* found in the earlier list. (The SCOWL was really only needed to “reach” *megachiropteran*; with the exception of it and *nonuniversalist*, all of the other examples in this post are valid Scrabble words!). The resulting word lists and corresponding counts of occurrences in the Ngrams data set are available here.

The resulting list of anagrams are in the text file linked below, sorted by MCSP score, then by the average frequency of the pair of words in each anagram. Interesting examples high on the list are (*personality*, *antileprosy*) with a score of 11, (*industries*, *disuniters*) with a score of 10, etc.

The full list of 82,776 anagrams sorted by MCSP and frequency

The individual word frequencies are included in the list, to allow investigation of other sorting methods. For example, it might be useful to normalize MCSP score by word length. Or instead of using the *average* frequency of the two words in an anagram, the *ratio* of frequencies would more heavily weight anagrams between a really common word and a relatively unknown one, such as (*penalties*, *antisleep*)– I have never heard of the latter, but both are Scrabble-playable.

**References:**

- Dominus, M., I found the best anagram in English,
*The Universe of Discourse*, 21 February 2017 [HTML] - Goldstein, A., Kolman, P., Zheng, J., Minimum Common String Partition Problem: Hardness and Approximations,
*Electronic Journal of Combinatorics*,**12**(2005), #R50 [PDF]

]]>

sz = [3, 4, 2]; block = reshape(0:(prod(sz(2:end)) - 1), sz(2:end)); a = []; for k = 1:sz(1) a(k,:,:) = block; block = block + numel(block); end

As this example shows, MATLAB allows resizing arrays on the fly, so to speak; assignment to an element– or in this case, a range of elements– whose indices would normally be out of bounds automatically re-allocates memory to accommodate the new, larger array. However, these incremental re-allocation and re-copying operations can be costly in terms of execution time. Granted, this toy example is small enough not to matter, but in the “real” example, the eventual array size was approximately `sz=[256,16384,4]`

, in which case executing the above code takes about 8 seconds on my laptop.

Pre-allocation is usually recommended as a fix: that is, size the entire array in advance, before assigning any elements. But what if you don’t *know* the size of the array in advance? Although there are several approaches to dealing with this situation, with varying complexity, the subject of this post is to describe just how much execution time may be eliminated by only a modest change to the above code.

A major source of slowness is that the array expansion occurs along the *first* dimension… which in MATLAB, with its Fortran roots, is the index that changes *most* frequently in the underlying block of memory containing the array elements. So not only must we re-allocate memory to accommodate each *new* sub-array, even copying the *existing* array elements is a non-trivial task, as the following figure shows. As we “append” first the cyan block, then the red block, then the green block, each incremental re-allocation also requires “re-interleaving” the old and new elements in the new larger block of memory:

Instead, consider the following code, modified to append each block along the *last*— and most *slowly* changing– dimension, followed by the appropriate transposition via `permute`

to shuffle everything back into the originally intended shape:

a = []; for k = 1:sz(1) a(:,:,k) = block; block = block + numel(block); end a = permute(a, [3, 1, 2]);

The effect of this change is that, although we still incur all of the incremental re-allocation cost, each memory *copy* operation is a more straightforward and much faster “blit”:

This version executes in less than a quarter of a second on my laptop, about 35 times faster than the original 8 seconds.

]]>

I am thinking of a randomly selected integer between 1 and (inclusive). You are the first of players who will each, in turn, get a single guess at the selected number. The player whose guess is closest wins $300, with ties splitting the winnings evenly.

Here is the catch: each player may not guess a number that has already been guessed by a previous player. As the first player, what should your strategy be? Which player, if any, has the advantage? And what happens if we vary and ?

]]>

Let’s focus the problem a bit, and just consider the *first* turn of the game, when there are no other tiles on the board: what is the probability that 7 randomly drawn Scrabble tiles may be played to form a valid 7-letter word?

There are , or over 16 billion equally likely ways to draw a rack of 7 tiles from the 100 tiles in the North American version of the game. But since some tiles are duplicated, there are only 3,199,724 distinct possible racks (not necessarily equally likely). Which of these racks form valid words?

It depends on what we mean by *valid*. According to the 2014 *Official Tournament and Club Word List* (the latest for which an electronic version is accessible), there are 25,257 playable words with 7 letters… but many of those are words that I don’t even *know*, let alone expect to be able to recognize from a scrambled rack of tiles. We need a way to reduce this over-long official list of words down to a “novice” list of words– or better yet, *rank* the entire list from “easiest” to “hardest,” and compute the desired probability as a function of the size of the accepted dictionary.

The Google Books Ngrams data set (English version 20120701) provides a means of doing this. As we have done before (described here and here), we can map each 7-letter Scrabble word to its frequency of occurrence in the Google Books corpus, the idea being that “easier” words occur more frequently than “harder” words.

The following figure shows the sorted number of occurrences of all 7-letter Scrabble words on a logarithmic scale, with some highlighted examples, ranging from *between*, the single most frequently occurring 7-letter Scrabble word, to *simioid*, one of the least frequently occurring words… and this doesn’t even include the 1444 playable words– about 5.7% of the total– that appear *nowhere* in the entire corpus, such as *abaxile* and *zygoses*.

Armed with this sorted list of 25,257 words, we can now compute, as a function of , the probability that a randomly drawn rack of 7 tiles may be played to form one of the easiest words in the list. Following is Mathematica code to compute these probabilities. This would be slightly simpler– and much more efficient– if not for the wrinkle of dealing with *blank* tiles, which allow multiple different words to be played from the same rack of tiles.

tiles = {" " -> 2, "a" -> 9, "b" -> 2, "c" -> 2, "d" -> 4, "e" -> 12, "f" -> 2, "g" -> 3, "h" -> 2, "i" -> 9, "j" -> 1, "k" -> 1, "l" -> 4, "m" -> 2, "n" -> 6, "o" -> 8, "p" -> 2, "q" -> 1, "r" -> 6, "s" -> 4, "t" -> 6, "u" -> 4, "v" -> 2, "w" -> 2, "x" -> 1, "y" -> 2, "z" -> 1}; {numBlanks, numTiles} = {" " /. tiles, Total[Last /@ tiles]}; racks[w_String] := Map[ StringJoin@Sort@Characters@StringReplacePart[w, " ", #] &, Map[{#, #} &, Subsets[Range[7], numBlanks], {2}]] draws[r_String] := Times @@ Binomial @@ Transpose[Tally@Characters[r] /. tiles] all = {}; p = Accumulate@Map[( new = Complement[racks[#], all]; all = Join[all, new]; Total[draws /@ new] ) &, words] / Binomial[numTiles, 7];

The results are shown in the following figure, along with another sampling of specific playable words. For example, if we include the entire official word list, the probability of drawing a playable 7-letter word is 21226189/160075608, or about 0.132601.

A coarse inspection of the list suggests that I confidently recognize only about 8 or 9 thousand– roughly a third– of the available words, meaning that *my* probability of playing all 7 of my tiles on the first turn is only about 0.07. In other words, instead of a first-turn bingo every 7.5 games or so on average, I should expect to have to wait nearly twice as long. We’ll see if I’m even *that* good.

]]>

This is the so-called gambler’s ruin problem. It is a relatively common exercise to show that if , the probability of ruin is 1, and if , then the probability is

But what if the wager is not just win-or-lose a dollar, but is instead specified by an arbitrary probability distribution of outcomes? For example, suppose that at each iteration, we may win any of units, with respective probabilities . The purpose of this post is to capture my notes on some seemingly less well-known results in this more general case.

(The application to my current study of blackjack betting is clear: we have shown that, at least for a shoe game, even if we *play* perfectly, we are still going to lose if we don’t vary our bet. We can increase our win rate by betting more in favorable situations… but a natural constraint is to limit our *risk of ruin*, or probability of going broke.)

Schlesinger (see Reference (2) below) gives the following formula for risk of ruin, due to “George C., published on p. 8 of ‘How to Win $1 Million Playing Casino Blackjack'”:

where and are the mean and standard deviation, respectively, of the outcome of each round (or hourly winnings). It is worth emphasizing, since it was unclear to me from the text, that this formula is an approximation, albeit a pretty good one. The derivation is not given, but the approach is simple to describe: normalize the units of both bankroll and outcome of rounds to have unit variance (i.e., divide everything by ), then use the standard two-outcome ruin probability formula above with win probability chosen to reflect the appropriate expected value of the round, i.e., .

The unstated assumption is that (note that ruin is guaranteed if , or if and ), and that accuracy of the approximation depends on , which is fortunately generally the case in blackjack.

There *is* an exact formula for risk of ruin, at least as long as outcomes of each round are bounded and integral. In Reference (1) below, Katriel describes a formula involving the roots inside the complex unit disk of the equation

where is the probability of winning units in each round. Execution time and numeric stability make effective implementation tricky.

Finally, just to have some data to go along with the equations, following is an example of applying these ideas to analysis of optimal betting in blackjack. Considering the same rules and setup as in the most recent posts (6 decks, S17, DOA, DAS, SPL1, no surrender, 75% penetration), let’s evaluate all possible betting ramps with a 1-16 spread through a maximum (floored) true count of 10, for each of five different betting and playing strategies, ranging from simplest to most complex:

- Fixed basic “full-shoe” total-dependent zero-memory strategy (TDZ), using Hi-Lo true count for betting only.
- Hi-Lo with the Illustrious 18 indices.
- Hi-Lo with full indices.
- Hi-Opt II with full indices and ace side count.
- “Optimal” betting and playing strategy, where playing strategy is CDZ- optimized for each pre-deal depleted shoe, and betting strategy is ramped according to the corresponding exact pre-deal expected return.

Then assuming common “standard” values of $10,000 initial bankroll, a $10 minimum bet, and 100 hands per hour, the following figure shows the achievable win rate ($ per hour) and corresponding risk of ruin for each possible strategy and betting ramp:

There are plenty of interesting things to note and investigate here. The idea is that we can pick a maximum acceptable risk of ruin– such as the red line in the figure, indicating the standard Kelly-derived value of , or about 13.5%– and find the betting ramp that maximizes win rate without exceeding that risk of ruin. Those best win rates for this particular setup are:

- Not achievable for TDZ (see below).
- $20.16/hr for Hi-Lo I18.
- $21.18/hr for Hi-Lo full.
- $26.51/hr for Hi-Opt II.
- $33.09/hr for optimal play.

Fixed basic TDZ strategy, shown in purple, just isn’t good enough; that is, there *is no* betting ramp with a risk of ruin smaller than about 15%. And some betting ramps, even with the 1-16 spread constraint, still yield a negative overall expected return, resulting in the “tail” at P(ruin)=1. (But that’s using Hi-Lo true count as the “input” to the ramp; it is possible that “perfect” betting using exact pre-deal expected return could do better.)

**References:**

- Katriel, Guy, Gambler’s ruin probability – a general formula, arXiv:1209.4203v4 [math.PR], 2 July 2013
- Schlesinger, Don, Blackjack Attack: Playing the Pros’ Way, 3rd ed. Las Vegas: RGE Publishing, Ltd., 2005

]]>

- Its solution involves looking at the problem in a non-obvious way, in this case by considering the
*complementary*event that all birthdays are*distinct*. - Once the approach in (1) is understood, computing the answer is relatively easy: the probability is , where is the falling factorial.
- The answer is surprising. When I ask students, “How many people are needed for the probability of a shared birthday to exceed 1/2?”, guesses as high as 180 are common.
- Picking on the not-quite-realistic assumption of all birthdays being equally likely actually
*helps*; that is, with a non-uniform distribution of birthdays, the probability of coincidence is*higher*.

But what about larger ? For example, suppose that while surveying your students’ birthdays in preparation for this problem, you find that *three* of them share a birthday? What is the probability of this happening?

The Wikipedia page on the birthday problem doesn’t address this generalization at all. There are several blog and forum posts addressing the case specifically, by grouping the prohibited cases according to the number of *pairs* of people with shared birthdays. This is generalized to arbitrary on the Wolfram MathWorld page; the resulting recurrence relation is pretty complex, but it’s a nice exercise to prove that it works.

The motivation for this post is to describe what I think is a relatively simpler solution, for arbitrary , including Python source code to perform the calculation. Let’s fix the number of equally likely possible birthdays , and the desired number of people sharing a birthday, and define the function

Then is the exponential generating function for the number of “prohibited” assignments of birthdays to people where *no more* than share a birthday. That is, the number of such prohibited assignments is times the coefficient of in .

(When working through *why* this works, it’s interesting how often it can be helpful to transform the problem into a different context. For example, in this case, we are also counting the number of length- strings over an alphabet of characters, where no character appears more than times.)

The rest of the calculation follows in the usual manner: divide by the *total* number of possible assignments to get the complementary probability, then subtract from 1. The following Python code performs this calculation, either exactly– using the `fractions`

module, which can take a while– or in double precision, which is much faster.

import numpy as np from numpy.polynomial import polynomial as P import fractions import operator def p(k, n, d=365, exact=False): f = fractions.Fraction if exact else operator.truediv q = P.polypow(np.array( [f(1, np.math.factorial(j)) for j in range(k)], dtype=object), d)[n] for j in range(1, n + 1): q = q * f(j, d) return 1 - q

]]>

Often the first exposure to induction is the so-called “weak” form: to show that is true for all , it suffices to prove the following two claims:

**Base step**: is true.**Induction step**: For all , if is true, then is true.

That is,

So far, so good. Although this is technically all that we need, that this is referred to as “weak” induction suggests that there is a stronger form. There is… sort of. In his *Applied Combinatorics* (see Reference 1 below), Tucker skips weak induction altogether, presenting induction in the following “strong” form:

**Base step**: is true.**Induction step**: If are true, then is true.

The idea is that the induction hypothesis is stronger, which can make the resulting proof simpler; in the process of proving in the induction step, we have at our disposal not only the assumed truth of , but also *all* of the other previous statements. (I say “sort of,” and wrap “weak” and “strong” in quotes, because the two forms are actually equivalent. Strong induction isn’t really any stronger in the sense of more “proving power”; statements provable using strong induction are also provable by weak induction, and vice versa.)

Notice that the induction step above, quoted directly from Tucker, lacks some rigor in that there is no explicit universal quantifier for . That is, for what values of do we need to demonstrate the induction step? This is important, because it turns out that if we are more careful, we can economize a bit and demonstrate that is true for all by proving the single more compact claim:

**Strong induction “single-step”**: For all , if is true for all , then is also true.

That is,

Velleman (Reference 2) takes this presentation approach, which certainly has elegance going for it. But he then goes on to “note that *no base case is necessary in a proof by strong induction* [my emphasis].” This is where I think things can get confusing. It’s certainly true that, *once* we have demonstrated the implication in the single-step version above, *then* we know that is true. As Velleman explains, “plugging in 0 for , we can conclude that . But because there are no natural numbers smaller than 0, the statement is vacuously true. Therefore, by *modus ponens*, is true. (*This explains why the base case doesn’t have to be checked separately in a proof by strong induction* [my emphasis]; the base case actually follows from the modified form of the induction step used in strong induction.)”

There is nothing strictly incorrect here. What may be misleading, though, is the repeated assurance that no special attention is required for treatment of the base case. When writing the actual proof of the implication in the single-step version of strong induction, the argument may “look different” for than it does for . I think Wikipedia actually does the most admirable job of explaining this wrinkle: “*Sometimes* the same argument applies for and , making the proof simpler and more elegant. In this method it is, however, vital to ensure that the proof of does not implicitly assume that , e.g. by saying “choose an arbitrary ” or assuming that a set of elements *has* an element [my emphasis added].”

From a teaching perspective, what are good examples of induction proofs that highlight these issues? This is a good question. When discussing strong induction, the most common example seems to be the existence part of the fundamental theorem of arithmetic: every integer greater than 1 is the product of one or more primes. Here we really can make just one argument, so to speak, without treating the first prime number 2 as a special base case (interestingly, Wikipedia *does* address it separately).

In a course on graph theory, I think another nice example is the proof that any round-robin tournament contains a Hamiltonian path. Again, the single-step version of a strong induction argument works here as well (although we have to be a little more careful about what happens when some constructed subsets of vertices end up being empty).

Finally, another common example where strong induction is useful, but special treatment of base case(s) *is* required, involves postage stamps: show that it is possible to use 4- and 5-cent stamps to form every amount of postage of 12 cents or more.

**References:**

- Tucker, Alan, Applied Combinatorics, 6th ed. New York: Wiley and Sons, 2012
- Velleman, Daniel J., How to Prove It: A Structured Approach. New York: Cambridge University Press, 2006

]]>

Each of the six faces is uniquely labeled with the integers 1 through 6. But that’s not all; note that the values on opposite faces always sum to 7. This is a standard arrangement applied more generally to other types of dice as well. My set of Platonic solid dice– with sides for — all have this same property. Let us call such an -sided die *standard* if all opposite faces sum to .

**Problem 1:** How many different standard 6-sided dice are there? That is, in how many ways can we label the faces with distinct integers 1 through 6, with opposite faces summing to 7?

**Problem 2:** What if we relax the standard (constant opposite sum) property? That is, in how many ways can we label the faces with distinct integers 1 through 6, with no other restrictions?

**Problem 3:** Same as Problem 2, but for the other Platonic solids (d4, d8, d12, d20)?

I think these are great examples of problems for students that straddle whatever line there may be between mathematics and computer science. Problems 1 and 2 can be solved as-is “by hand.” (And they are interesting in part because the answers are perhaps surprisingly small numbers.) But the usual mathematical machinery involved just *counts* the number of dice in each case; it’s an interesting extension as a programming problem to actually *enumerate* (i.e., list) them, display visual representations of them, etc.

Problem 3 is more challenging; the machinery is the same, but the larger numbers involved require some amount of automation in the housekeeping.

Now for what I think is a really hard problem motivating this post (*Edit*: although after a response from a reader in another forum with a very elegant solution, perhaps this is not as difficult as I thought it might be!):

**Problem 4:** Same as Problem 1, but for the other Platonic solids– that is, how many different *standard* (d4, d8, d12, d20) are there, with constant opposite sums?

]]>