{ "cells": [ { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# Sicherman Dice\n", "\n", "*Note: This notebook takes the form of a conversation between two problem solvers. One speaks in* **bold**, *the other in* plain. *Also note, for those who are not native speakers of English: \"dice\" is the plural form; \"die\" is the singular.*\n", "\n", "Huh. This is interesting. You know how in many games, such as craps or Monopoly, you roll two regular dice and add them up. Only the sum matters, not what either of the individual dice shows.\n", "\n", "**Right.**\n", "\n", "And some of those sums, like 8, can be made multiple ways, while 2 and 12 can only be made one way. \n", "\n", "**Yeah. 8 can be made 5 ways, so it has a 5/36 probability of occuring.**\n", "\n", "The interesting thing is that people have been playing dice games for 7,000 years. But it wasn't until 1977 that Colonel George Sicherman asked whether is is possible to have a pair of dice that are not regular dice—that is, they don't have (1, 2, 3, 4, 5, 6) on the six sides—but have the same distribution of sums as a regular pair—so the pair of dice would also have to have 5 ways of making 8, but it could be different ways; maybe 7+1 could be one way. Sicherman assumes that each side bears a positive integer.\n", "\n", "**And what did he find?**\n", "\n", "Wouldn't it be more fun to figure it out for ourselves?\n", "\n", "**OK!**\n", "\n", "How could we proceed?\n", "\n", "**When in doubt, [use brute force](http://quotes.lifehack.org/quote/ken-thompson/when-in-doubt-use-brute-force/): we can write a program to enumerate the possibilities:**\n", "\n", "\n", "- **Generate all dice that could possibly be part of a solution, such as (1, 2, 2, 4, 8, 9).**\n", "- **Consider all pairs of these dice, such as ((1, 3, 4, 4, 5, 8), (1, 2, 2, 3, 3, 4))**\n", "- **See if we find any pairs that are not the regular pair, but do have the same distribution of sums as the regular pair.**\n", "\n", "\n", "That's great. I can code up your description almost verbatim. I'll also keep track of our TO DO list:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def sicherman():\n", " \"\"\"The set of pairs of 6-sided dice that have the same\n", " distribution of sums as a regular pair of dice.\"\"\"\n", " return {pair for pair in pairs(all_dice())\n", " if pair != regular_pair\n", " and sums(pair) == regular_sums}\n", "\n", "# TODO: pairs, all_dice, regular_pair, sums, regular_sums" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**Looks good to me.**\n", "\n", "Now we can tick off the items in the TO DO list. The function pairs is first, and it is easy:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def pairs(collection): \n", " \"Return all pairs (A, B) from collection where A <= B.\"\n", " return [(A, B) for A in collection for B in collection if A <= B]\n", "\n", "# TODO: all_dice, regular_pair, sums, regular_sums" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**That's good. We could have used the library function itertools.combinations_with_replacement, but let's just leave it as is. We should test to make sure it works:**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pairs(['A', 'B', 'C'])" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# TO DO: sums(pair)\n", "\n", "\n", "Now for sums: we need some way to represent all the 36 possible sums from a pair of dice. We want a representation that will be the same for two different pairs if all 36 sums are the same, but where the order or composition of the sums doesn't matter. \n", "\n", "**So we want a set of the sums?**\n", "\n", "Well, it can't be a set, because we need to know that 8 can be made 5 ways, not just that 8 is a member of the set. The technical term for a collection where order doesn't matter but where you can have repeated elements is a **bag**, or sometimes called a [**multiset**](https://en.wikipedia.org/wiki/Multiset). For example, the regular pair of dice makes two 11s with 5+6 and 6+5, and another pair could make two 11s with 7+4 and 3+8. Can you think of a representation that will do that?\n", "\n", "**Well the easiest is just a sorted list or tuple—if we don't want order to matter, sorting takes care of that. Another choice would be a dictionary of {sum: count} entries, like {2: 1, 3: 2, ... 11: 2, 12: 1}. There is even a library class, collections.Counter, that does exactly that.**\n", "\n", "How do we choose between the two representations?\n", "\n", "**I don't think it matters much. Since there are only 36 entries, I think the sorted list will be simpler, and probably more efficient. For 100-sided dice I'd probably go with the Counter.**\n", "\n", "OK, here's some code implementing sums as a sorted list, and definitions for regular die pair, and sums.\n", "By the way, I could have used range(1, 7) to define a regular die, but range is zero-based, and regular dice are one-based, so I defined the function ints instead." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def sums(pair):\n", " \"All possible sums of a side from one die plus a side from the other.\"\n", " (A, B) = pair\n", " return Bag(a + b for a in A for b in B)\n", "\n", "Bag = sorted # Implement a bag as a sorted list\n", "\n", "def ints(start, end): \n", " \"A tuple of the integers from start to end, inclusive.\"\n", " return tuple(range(start, end + 1))\n", "\n", "regular_die = ints(1, 6)\n", "regular_pair = (regular_die, regular_die)\n", "regular_sums = sums(regular_pair)\n", "\n", "# TODO: all_dice" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Let's check the regular_sums:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "36" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(regular_sums)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 12]\n" ] } ], "source": [ "print(regular_sums)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**And we can see what that would look like to a Counter:**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Counter({2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 5, 9: 4, 10: 3, 11: 2, 12: 1})" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from collections import Counter\n", "\n", "Counter(regular_sums)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**Looks good! Now only one more thing on our TODO list:**\n", "\n", "# TO DO: all_dice()\n", "\n", "\n", "all_dice should generate all possible dice, where by \"possible\" I mean the dice that could feasibly be part of a pair that is a solution to the Sicherman problem. Do we know how many dice that will be? Is it a large enough number that efficiency will be a problem?\n", "\n", "**Let's see. A die has six sides each. If each side can be a number from, say, 1 to 10, that's 106 or a million possible dice; a million is a small number for a computer.**\n", "\n", "True, a million is a relatively small number for all_dice(), but how many pairs(all_dice()) will there be?\n", "\n", "**Ah. A million squared is a trillion. That's a large number even for a computer. Just counting to a trillion takes hours in Python; checking a trillion pairs will take days.**\n", "\n", "So we need to get rid of most of the dice. What about permutations?\n", "\n", "**Good point. If I have the die (1, 2, 3, 4, 5, 6), then I don't need the 6! = 720 different permutations of this die— that is, dice like (2, 4, 6, 1, 3, 5).\n", "Each die should be a bag (I learned a new word!) of sides. So we've already eliminated 719/720 = 99.9% of the work.**\n", "\n", "One other thing bothers me ... how do you know that the sides can range from 1 to 10? Are you sure that 11 can't be part of a solution? Or 12?\n", "\n", "**Every side on every die must be a positive integer, right?**\n", "\n", "Right. No zeroes, no negative numbers, no fractions.\n", "\n", "**Then I know for sure that 12 can't be on any die, because when you add 12 to whatever is on the other die, you would get at least 13, and 13 is not allowed in the regular distribution of sums.**\n", "\n", "Good. How about 11?\n", "\n", "**We can't have a sum that is bigger than 12. So if one die had an 11, the other would have to have all 1s. That wouldn't work, because then we'd have six 12s, but we only want one. So 10 is the biggest allowable number on a die.**\n", "\n", "What else can we say about the biggest number on a die?\n", "\n", "**There's one 12 in the sums. But there are several ways to make a 12: 6+6 or 7+5 or 8+4, and so on. So I can't say for sure what the biggest number on any one die will be. But I can say that whatever the biggest number on a die is, it will be involved in summing to 12, so there can be only one of them, because we only want to make one 12.**\n", "\n", "What about the smallest number on a die?\n", "\n", "**Well, there's only one 2 allowed in the sums. The only way to sum to 2 is 1+1: a 1 from each of the dice in the pair. If a die had no 1s, we wouldn't get a 2; if a die had more than one 1, we would get too many 2s. So every die has to have exactly one 1.**\n", "\n", "Good. So each die has exactly one 1, and exactly one of whatever the biggest number is, something in the range up to 10. Here's a picture of the six sides of any one die:\n", "\n", " 1  <\n", "2-10 ≤\n", "2-10 ≤\n", "2-10 ≤\n", "2-10 <\n", "2-10\n", "\n", "The bag of sides is always listed in non-decreasing order; the first side, 1, is less than the next, and the last side, whatever it is, is greater than the one before it.\n", "\n", "**Wait a minute: you have [2-10] < [2-10]. But 2 is not less than 2, and 10 is not less than 10. I think it should be [2-9] < [3-10]. So the picture should be like this:**\n", "\n", "\n", " 1  <\n", "2-9 ≤\n", "2-9 ≤\n", "2-9 ≤\n", "2-9 <\n", "3-10\n", "\n", "Good! We're making progress in cutting down the range. But it That this bothers me because it says the range for the biggest number is 3 to 10. But if one die has a 3 and the other a 10, that adds to 13. So I'm thinking that it is not possible to have a 10 after all—because if one die had a 10, then the other would have to have a 2 as the biggest number, and that can't be. Therefore the biggest number is in the range of 3 to 9. But then the others have to be \n", "less, so make them 2 to 8:\n", "\n", " 1  <\n", "2-8 ≤\n", "2-8 ≤\n", "2-8 ≤\n", "2-8 <\n", "3-9\n", "\n", "**I can turn this picture into code:**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def all_dice():\n", " \"A list of all feasible 6-sided dice for the Sicherman problem.\"\n", " return [(1, s2, s3, s4, s5, s6)\n", " for s2 in ints(2, 8) \n", " for s3 in ints(s2, 8)\n", " for s4 in ints(s3, 8)\n", " for s5 in ints(s4, 8) \n", " for s6 in ints(s5+1, 9)]" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "I think we're ready to run sicherman(). Any bets on what we'll find out?\n", "\n", "**I bet that Sicherman is remembered because he discovered a pair of dice that works. If he just proved the non-existence of a pair, I don't think that would be noteworthy.**\n", "\n", "Makes sense. Here goes:\n", "\n", "# The Answer" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sicherman()" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**Look at that!**\n", "\n", "It turns out you can buy a pair of dice with just these numbers.\n", "\n", "\n", "\n", "Here's a table I borrowed from [Wikipedia](https://en.wikipedia.org/wiki/Sicherman_dice) that shows both pairs of dice have the same sums. \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
 2 3 4 5 6 7 8 9 10 11 12 Regular dice:\n", "(1, 2, 3, 4, 5, 6)\n", "(1, 2, 3, 4, 5, 6) 1+1 1+2\n", "2+1 1+3\n", "2+2\n", "3+1 1+4\n", "2+3\n", "3+2\n", "4+1 1+5\n", "2+4\n", "3+3\n", "4+2\n", "5+1 1+6\n", "2+5\n", "3+4\n", "4+3\n", "5+2\n", "6+1 2+6\n", "3+5\n", "4+4\n", "5+3\n", "6+2 3+6\n", "4+5\n", "5+4\n", "6+3 4+6\n", "5+5\n", "6+4 5+6\n", "6+5 6+6 Sicherman dice:\n", "(1, 2, 2, 3, 3, 4)\n", "(1, 3, 4, 5, 6, 8) 1+1 2+1\n", "2+1 3+1\n", "3+1\n", "1+3 1+4\n", "2+3\n", "2+3\n", "4+1 1+5\n", "2+4\n", "2+4\n", "3+3\n", "3+3 1+6\n", "2+5\n", "2+5\n", "3+4\n", "3+4\n", "4+3 2+6\n", "2+6\n", "3+5\n", "3+5\n", "4+4 1+8\n", "3+6\n", "3+6\n", "4+5 2+8\n", "2+8\n", "4+6 3+8\n", "3+8 4+8
\n", "\n", "We could stop here. Or we could try to solve it for *N*-sided dice.\n", "\n", "# Why stop now? Onward!" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "OK. I know 4-, 12-, and 20-sided dice are common, but we'll try to handle any *N* > 1. My guess is we won't go too far before our program becomes too slow. So, before we try *N*-sided dice, let's analyze six-sided dice a little better, to see if we can eliminate some of the pairs before we start. The picture says that (1, 2, 2, 2, 2, 3) could be a valid die. Could it?\n", "\n", "**No! If a die had four 2s, then we know that since the other die has one 1, we could make 2 + 1 = 3 four ways. But the regular_sums has only two 3s. So that means that a die can have no more than two 2s. New picture:**\n", "\n", " 1  <\n", "2-8 ≤\n", "2-8 ≤\n", "3-8 ≤\n", "3-8 <\n", "3-9\n", "\n", "Now we've got [3-8] < [3-9]; that's not right. If a die can only have one 1 and two 2s, then it must have at least one number that is a 3 or more, followed by the biggest number, which must be 4 or more, and we know a pair of biggest numbers must sum to 12, so the range of the biggest can't be [4-9], it must be [4-8]:\n", "\n", " 1  <\n", "2-7 ≤\n", "2-7 ≤\n", "3-7 ≤\n", "3-7 <\n", "4-8" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "button": false, "collapsed": true, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def all_dice():\n", " \"A list of all feasible 6-sided dice for the Sicherman problem.\"\n", " return [(1, s2, s3, s4, s5, s6)\n", " for s2 in ints(2, 7) \n", " for s3 in ints(s2, 7)\n", " for s4 in ints(max(s3, 3), 7)\n", " for s5 in ints(s4, 7) \n", " for s6 in ints(s5+1, 8)]" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "I'll count how many dice and how many pairs there are now:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "231" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(all_dice())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "26796" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(pairs(all_dice()))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**Nice—we got down from a trillion pairs to 26,000. I don't want to print all_dice(), but I can sample a few:**" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[(1, 5, 5, 5, 7, 8),\n", " (1, 6, 6, 6, 7, 8),\n", " (1, 3, 4, 6, 6, 7),\n", " (1, 3, 3, 7, 7, 8),\n", " (1, 3, 5, 5, 6, 8),\n", " (1, 2, 2, 4, 4, 5),\n", " (1, 2, 5, 6, 7, 8),\n", " (1, 3, 4, 4, 4, 8),\n", " (1, 2, 3, 5, 5, 6),\n", " (1, 2, 2, 3, 3, 8)]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import random\n", "\n", "random.sample(all_dice(), 10)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# sicherman(N)\n", "\n", "OK, I think we're ready to update sicherman() to sicherman(N). \n", "\n", "**Sure, most of that will be easy, just parameterizing with N:**" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def sicherman(N=6):\n", " \"\"\"The set of pairs of N-sided dice that have the same\n", " distribution of sums as a regular pair of N-sided dice.\"\"\"\n", " reg_sums = regular_sums(N)\n", " reg_pair = regular_pair(N)\n", " return {pair for pair in pairs(all_dice(N))\n", " if pair != reg_pair\n", " and sums(pair) == reg_sums}\n", "\n", "def regular_die(N): return ints(1, N)\n", "def regular_pair(N): return (regular_die(N), regular_die(N))\n", "def regular_sums(N): return sums(regular_pair(N))\n", "\n", "# TODO: all_dice(N)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Good. I think it would be helpful for me to look at a table of regular_sums:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "N: 1 {2: 1}\n", "N: 2 {2: 1, 3: 2, 4: 1}\n", "N: 3 {2: 1, 3: 2, 4: 3, 5: 2, 6: 1}\n", "N: 4 {2: 1, 3: 2, 4: 3, 5: 4, 6: 3, 7: 2, 8: 1}\n", "N: 5 {2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 4, 8: 3, 9: 2, 10: 1}\n", "N: 6 {2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 5, 9: 4, 10: 3, 11: 2, 12: 1}\n", "N: 7 {2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, 9: 6, 10: 5, 11: 4, 12: 3, 13: 2, 14: 1}\n" ] } ], "source": [ "for N in ints(1, 7):\n", " print(\"N:\", N, dict(Counter(regular_sums(N))))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**That is helpful. I can see that any regular_sums must have one 2 and two 3s, and three 4s, and so on, not just for N=6 but for any N (except for trivially small N). And that means that any regular die can have at most two 2s, three 3s, four 4s, and so on. So we have this picture:**\n", "\n", " 1  <\n", "2+ ≤\n", "2+ ≤\n", "3+ ≤\n", "3+ ≤\n", "3+ ≤\n", "4+ ≤\n", "4+ ≤\n", "4+ ≤\n", "4+ ≤ ...\n", "\n", "**where [2+] means the lower bound is 2, but we haven't figured out yet what the upper bound is.**\n", "\n", "Let's figure out upper bounds starting from the biggest number. What can the biggest number be?\n", "\n", "**For a pair of *N*-sided die, the biggest sides from each one must add up to 2*N*. Let's take *N*=10 as an example. The biggest numbers on two 10-sided Sicherman dice must sum to 20. According to the picture above, the lower bound on the biggest number would be 4, but because there can only be one of the biggest number, the lower bound is 5. So to add up to 20, the range must be [5-15]:**\n", "\n", "\n", " 1  <\n", "2+ ≤\n", "2+ ≤\n", "3+ ≤\n", "3+ ≤\n", "3+ ≤\n", "4+ ≤\n", "4+ ≤\n", "4+ <\n", "5-15 \n", "\n", "There's probably some tricky argument for the upper bounds of the other sides, but I'm just going to say the upper bound is one less than the upper bound of the biggest number:\n", "\n", " 1  <\n", "2-14 ≤\n", "2-14 ≤\n", "3-14 ≤\n", "3-14 ≤\n", "3-14 ≤\n", "4-14 ≤\n", "4-14 ≤\n", "4-14 <\n", "5-15 \n", "\n", "\n", "Let's start by coding up lower_bounds(N):" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "button": false, "collapsed": true, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def lower_bounds(N):\n", " \"A list of lower bounds for respective sides of an N-sided die.\"\n", " lowers = [1]\n", " for _ in range(N-1):\n", " m = lowers[-1] # The last number in lowers so far\n", " lowers.append(m if (lowers.count(m) < m) else m + 1)\n", " lowers[-1] = lowers[-2] + 1\n", " return lowers" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 2, 3, 3, 4]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lower_bounds(6)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 2, 3, 3, 3, 4, 4, 4, 5]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lower_bounds(10)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "And upper_bounds(N):" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "button": false, "collapsed": true, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def upper_bounds(N):\n", " \"A list of upper bounds for respective sides of an N-sided die.\"\n", " U = 2 * N - lower_bounds(N)[-1]\n", " return [1] + (N - 2) * [U - 1] + [U]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[1, 7, 7, 7, 7, 8]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "upper_bounds(6)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[1, 14, 14, 14, 14, 14, 14, 14, 14, 15]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "upper_bounds(10)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Now, what do we have to do for all_dice(N)? When we knew we had six sides, we wrote six nested loops. We can't do that for *N*, so what do we do?\n", "\n", "**Here's an iterative approach: we keep track of a list of partially-formed dice, and on each iteration, we add a side to all the partially-formed dice in all possible ways, until the dice all have N sides. So for eaxmple, we'd start with:**\n", "\n", " dice = [(1,)]\n", " \n", "**and then on the next iteration (let's assume *N*=6, so the lower bound is 2 and the upper bound is 7), we'd get this:**\n", "\n", " dice = [(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7)]\n", " \n", "**on the next iteration, we find all the ways of adding a third side, and so on. Like this:**" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def all_dice(N):\n", " \"Return a list of all possible N-sided dice for the Sicherman problem.\"\n", " lowers = lower_bounds(N)\n", " uppers = upper_bounds(N)\n", " def possibles(die, i):\n", " \"The possible numbers for the ith side of an N-sided die.\"\n", " return ints(max(lowers[i], die[-1] + int(i == N-1)),\n", " uppers[i])\n", " dice = [(1,)]\n", " for i in range(1, N):\n", " dice = [die + (side,)\n", " for die in dice\n", " for side in possibles(die, i)]\n", " return dice" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**The tricky part was with the max: the actual lower bound at least lowers[i], but it must be as big as the previous side, die[-1]. And just to make things complicated, the very last side has to be strictly bigger than the previous; \" + int(i == N-1)\" does that by adding 1 just in case we're on the last side, and 0 otherwise.**\n", "\n", "**Let's check it out:**" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "231" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(all_dice(6))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Reassuring that we get the same number we got with the old version of all_dice()." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[(1, 2, 2, 3, 4, 6),\n", " (1, 3, 5, 5, 6, 8),\n", " (1, 3, 3, 6, 7, 8),\n", " (1, 2, 3, 3, 3, 7),\n", " (1, 3, 5, 5, 7, 8),\n", " (1, 2, 4, 5, 6, 8),\n", " (1, 3, 6, 6, 7, 8),\n", " (1, 3, 4, 5, 5, 7)]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.sample(all_dice(6), 8)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# Running sicherman(N) for small N\n", "\n", "Let's try sicherman for some small values of N:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{2: set(),\n", " 3: set(),\n", " 4: {((1, 2, 2, 3), (1, 3, 3, 5))},\n", " 5: set(),\n", " 6: {((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))}}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{N: sicherman(N)\n", " for N in ints(2, 6)}" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Again, reassuring that we get the same result for sicherman(6). And interesting that there is a result for sicherman(4) but not for the other *N*.\n", "\n", "Let's go onwards from *N*=6, but let's check the timing as we go:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 304 ms, sys: 2.73 ms, total: 307 ms\n", "Wall time: 519 ms\n" ] }, { "data": { "text/plain": [ "{((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time sicherman(6)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 18.2 s, sys: 209 ms, total: 18.4 s\n", "Wall time: 21.4 s\n" ] }, { "data": { "text/plain": [ "set()" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time sicherman(7)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# Estimating run time of sicherman(N) for larger N\n", "\n", "OK, it takes 50 or 60 times longer to do 7, compared to 6. At this rate, *N*=8 will take 15 minutes, 9 will take 15 hours, and 10 will take a month.\n", "\n", "**Do we know it will continue to rise at the same rate? You're saying the run time is exponential in *N*? **\n", "\n", "I think so. The run time is proportional to the number of pairs. The number of pairs is proportional to the square of the number of dice. The number of dice is roughly exponential in *N*, because each time you increase *N* by 1, you have to try a number of new sides that is similar to the number for the previous side (but not quite the same). I should plot the number of pairs on a log scale and see if it looks like a straight line.\n", "\n", "I can count the number of pairs without explicitly generating the pairs. If there are *D* dice, then the number of pairs is what? Something like *D* × (*D* + 1) / 2? Or is it *D* × (*D* - 1) / 2?\n", "\n", "**Let's draw a picture. With *D* = 4, here are all the ways to pair one die with another to yield 10 distinct pairs:**\n", " \n", " 11 .. .. ..\n", " 21 22 .. ..\n", " 31 32 33 ..\n", " 41 42 43 44\n", " \n", "**To figure out the formula, add a row to the top:**\n", "\n", " .. .. .. ..\n", " 11 .. .. ..\n", " 21 22 .. ..\n", " 31 32 33 ..\n", " 41 42 43 44\n", " \n", "**Now we have a *D* × (*D* + 1) rectangle, and we can see that half (10) of them are pairs, and half (the other 10) are not pairs (because they would be repetitions). So the formula is *D* × (*D* + 1)/2, and checking for *D*=4, (4 × 5) / 2 = 10, so we're good.**\n", " \n", "OK, let's try it. First some boilerplate for plotting:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import matplotlib\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "def logplot(X, Y, *options):\n", " \"Plot Y on a log scale vs X.\"\n", " fig, ax = plt.subplots()\n", " ax.set_yscale('log')\n", " ax.plot(X, Y, *options)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Now we can plot and display the number of pairs:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{2: 1,\n", " 3: 1,\n", " 4: 55,\n", " 5: 496,\n", " 6: 26796,\n", " 7: 1274406,\n", " 8: 17502486,\n", " 9: 823794345,\n", " 10: 37613250675,\n", " 11: 1688821500366,\n", " 12: 25925418283128}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAECCAYAAAAW+Nd4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xt0VOW5x/HvExWFWLUiWoOemNIilFY9HgVbUbGI2tpq\nqTdu2mhd2KqxFS9YNcY0bdVCtYoWixcCAuKtKFqqAjYoooLVVg82xUMDSEJVvFQNFzV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L3vya+\n/pDwR9473qrikfgU9G3CGW/OMrNdgCPcfSqAu3/SnrOZLmw7IN/Mtgd6EG6izBnuvhh4t8Xmk4Bp\nia+nAd+LtKgYtPY6uPsCd29MPHyOMNFmmzKi+TdnZvsBBwE5+fEeuBG4FIh/PC5eRcB6M5tqZi+a\n2RQz6x53UXFw9zrgN8Aawuy599x9QbxVZYQ93f0NCCeQwJ4x15MJzgb+1J4fzKjmb2Y7Aw8AP0l8\nAsgpibs7vskOAAABcElEQVSf30h8Cmo5kyrXbA8cDNzq7gcDGwgf83OOme1GOMstBAqAnbMpGTdC\nOX3CZGZXAh+7+6z2/HzGNP/Ex9kHgLvd/eG464nJ4cCJZvZP4B7gaDObvo3ndFVrgdfd/YXE4wcI\nbwa56Bjgn+7+TmJG3R+Ab8RcUyZ4w8z2AjCzLwBvxlxPbMysmDBc3O6Tgoxp/sBdwKvuflPchcTF\n3a9w9/9y9y8SLug96e5nxl1XHBIf5183s76JTUPJ3Yvga4DDzGwnMzPCa5GLF79bfhqeCxQnvv4B\nkCsnjZ95HczseMJQ8Ynuvrm9O8mI5m9mhwOjgW+a2UuJMV4tjS4XAjPN7K+E2T6/irmeWLj7UsIn\nn5eAvxH+8KfEWlTEWruvCLgOGGZm/yC8IV4XZ41RaON1mATsDMxP9M7ftWtfmTDPX0REopURZ/4i\nIhItNX8RkRyk5i8ikoPU/EVEcpCav4hIDlLzFxHJQWr+IiI56P8B+ADgb9sfomQAAAAASUVORK5C\nYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def plot_pairs(Ns):\n", " \"Given a list of N values, plot the number of pairs and return a dict of them.\"\n", " Ds = [len(all_dice(N)) for N in Ns]\n", " Npairs = [D * (D + 1) // 2 for D in Ds]\n", " logplot(Ns, Npairs, 'bo-')\n", " return {Ns[i]: Npairs[i] for i in range(len(Ns))}\n", "\n", "plot_pairs(ints(2, 12))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "OK, we've learned two things. One, it *is* roughly a straight line, so the number of pairs is roughly exponential. Two, there are a *lot* of pairs. 1014, just for *N*=12. I don't want to even think about *N*=20.\n", "\n", "**So if we want to get much beyond *N*=8, we're either going to need a brand new approach, or we need to make far fewer pairs of dice.**\n", "\n", "\n", "\n", "# Making Fewer pairs\n", "\n", "Maybe we could tighten up the upper bounds, but I don't think that will help very much.\n", "How about if we concentrate on making fewer pairs, without worrying about making fewer dice?\n", "\n", "**How could we do that? Isn't the number of pairs always (*D*2 + *D*)/2 ?**\n", "\n", "Remember, we're looking for *feasible* pairs. So if there was some way of knowing ahead of time that two dice were incompatible as a pair, we wouldn't even need to consider the pair.\n", "\n", "**By incompatible, you mean they can't form a pair that is a solution.**\n", "\n", "Right. Consider this: in any valid pair, the sum of the biggest number on each die must be 2*N*. For example, with *N* = 6:\n", "\n", " ((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8)) sum of biggests = 4 + 8 = 12\n", " ((1, 2, 3, 4, 5, 6), (1, 2, 3, 4, 5, 6)) sum of biggests = 6 + 6 = 12\n", " \n", "So if we have a die with biggest number 7, what dice should we consider pairing it with?\n", "\n", "**Only ones with biggest number 5.**\n", "\n", "**I get it: we sort all the die into bins labeled with their biggest number. Then we look at each bin, and for the \"7\" bin, we pair them up with the dice in the \"5\" bin. In general, the *B* bin can only pair with the 2*N* - *B* bin.**\n", "\n", "Exactly. \n", "\n", "**Cool. I can see how that can cut the amount of work by a factor of 10 or so. But I was hoping for a factor of a million or so.**\n", "\n", "There are other properties of a feasible pair.\n", "\n", "**Like what?**\n", "\n", "Well, what about the number of 2s in a pair?\n", "\n", "**Let's see. We know that any regular_sums has to have two 3s, and the only way to make a 3 is 2+1. And each die has only one 1, so that means that each pair of dice has to have a total of exactly two 2s.**\n", "\n", "Does it have to be one 2 on each die?\n", "\n", "**No. It could be one each, or it could be two on one die and none on the other. So a die with *T* twos can only pair with dice that have 2 - *T* twos.**\n", "\n", "Great. Can you think of another property?\n", "\n", "**Give me a hint.**\n", "\n", "Let's look at the sums of 6-sided Sicherman and regular pairs:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum((1, 2, 2, 3, 3, 4) + (1, 3, 4, 5, 6, 8))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum((1, 2, 3, 4, 5, 6) + (1, 2, 3, 4, 5, 6))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**They're the same. Is that [the question](http://hitchhikers.wikia.com/wiki/42) that 42 is the answer to? But does a Sicherman pair always have to have the same sum as a regular pair? I guess it doea, because the sum of sums(pair) is just all the sides added up *N* times each, so two pairs have the same sum of sums(pair) if and only if they have the same sum.**\n", "\n", "So consider the die (1, 3, 3, 3, 4, 5). What do we know about the dice that it can possibly pair with?\n", "\n", "**OK, that die has a biggest side of 5, so it can only pair with dice that have a biggest side of 12 - 5 = 7. It has a sum of 19, so it can only pair with dice that have a sum of 42 - 19 = 23. And it has no 2s, so it can only pair with dice that have two 2s.**\n", "\n", "I wonder how many such dice there are, out of all 231 all_dice(6)?" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{(1, 2, 2, 5, 6, 7)}" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{die for die in all_dice(6) if max(die) == 12 - 5 and sum(die) == 42 - 19 and die.count(2) == 2}" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**There's only 1. So, (1, 3, 3, 3, 4, 5) only has to try to pair with one die, rather than 230. Nice improvement!**\n", "\n", "In general, I wonder what the sum of the sides of a regular pair is?\n", "\n", "**Easy, that's N * (N + 1). [Gauss](http://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/) knew that when he was in elementary school!**\n", "\n", "# More efficient pairs(dice)\n", "\n", "**OK, we can code this up easily enough**:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "from collections import defaultdict\n", "\n", "def tabulate(dice):\n", " \"\"\"Put all dice into bins in a hash table, keyed by bin_label(die).\n", " Each bin holds a list of dice with that key.\"\"\"\n", " # Example: {(21, 6, 1): [(1, 2, 3, 4, 5, 6), (1, 2, 3, 4, 4, 7), ...]\n", " table = defaultdict(list)\n", " for die in dice:\n", " table[bin_label(die)].append(die)\n", " return table\n", "\n", "def pairs(dice):\n", " \"Return all pairs of dice that could possibly be a solution to the Sicherman problem.\"\n", " table = tabulate(dice)\n", " N = len(dice[0])\n", " for bin1 in table:\n", " bin2 = compatible_bin(bin1, N)\n", " if bin2 in table and bin1 <= bin2:\n", " for A in table[bin1]:\n", " for B in table[bin2]:\n", " yield (A, B)\n", "\n", "def bin_label(die): return sum(die), max(die), die.count(2)\n", "\n", "def compatible_bin(bin1, N): \n", " \"Return a bin label that is compatible with bin1.\"\n", " (S1, M1, T1) = bin1\n", " return (N * (N + 1) - S1, 2 * N - M1, 2 - T1)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**Let's make sure it works:**" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{2: set(),\n", " 3: set(),\n", " 4: {((1, 2, 2, 3), (1, 3, 3, 5))},\n", " 5: set(),\n", " 6: {((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))}}" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{N: sicherman(N)\n", " for N in ints(2, 6)}" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Good, those are the same answers as before. But how much faster is it?" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 24.9 ms, sys: 1.23 ms, total: 26.1 ms\n", "Wall time: 153 ms\n" ] }, { "data": { "text/plain": [ "set()" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time sicherman(7)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Wow, that's 1000 times faster than before." ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**I want to take a peek at what some of the bins look like:**" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "defaultdict(list,\n", " {(12, 4, 2): [(1, 2, 2, 3, 4)],\n", " (13, 4, 1): [(1, 2, 3, 3, 4)],\n", " (13, 5, 2): [(1, 2, 2, 3, 5)],\n", " (14, 4, 0): [(1, 3, 3, 3, 4)],\n", " (14, 5, 1): [(1, 2, 3, 3, 5)],\n", " (14, 5, 2): [(1, 2, 2, 4, 5)],\n", " (14, 6, 2): [(1, 2, 2, 3, 6)],\n", " (15, 5, 0): [(1, 3, 3, 3, 5)],\n", " (15, 5, 1): [(1, 2, 3, 4, 5)],\n", " (15, 6, 1): [(1, 2, 3, 3, 6)],\n", " (15, 6, 2): [(1, 2, 2, 4, 6)],\n", " (16, 5, 0): [(1, 3, 3, 4, 5)],\n", " (16, 5, 1): [(1, 2, 4, 4, 5)],\n", " (16, 6, 0): [(1, 3, 3, 3, 6)],\n", " (16, 6, 1): [(1, 2, 3, 4, 6)],\n", " (16, 6, 2): [(1, 2, 2, 5, 6)],\n", " (17, 5, 0): [(1, 3, 4, 4, 5)],\n", " (17, 6, 0): [(1, 3, 3, 4, 6)],\n", " (17, 6, 1): [(1, 2, 3, 5, 6), (1, 2, 4, 4, 6)],\n", " (18, 5, 0): [(1, 4, 4, 4, 5)],\n", " (18, 6, 0): [(1, 3, 3, 5, 6), (1, 3, 4, 4, 6)],\n", " (18, 6, 1): [(1, 2, 4, 5, 6)],\n", " (19, 6, 0): [(1, 3, 4, 5, 6), (1, 4, 4, 4, 6)],\n", " (19, 6, 1): [(1, 2, 5, 5, 6)],\n", " (20, 6, 0): [(1, 3, 5, 5, 6), (1, 4, 4, 5, 6)],\n", " (21, 6, 0): [(1, 4, 5, 5, 6)],\n", " (22, 6, 0): [(1, 5, 5, 5, 6)]})" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tabulate(all_dice(5))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "**Pretty good: four of the bins have two dice, but the rest have only one die.**\n", "\n", "And let's see how many pairs we're producing now. We'll tabulate *N* (the number of sides); *D* (the number of *N*-sided dice), the number pairs(dice) using the new pairs, and the number using the old pairs:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " N: D #pairs(dice) D*(D-1)/2\n", " 2: 1 1 0\n", " 3: 1 1 0\n", " 4: 10 3 45\n", " 5: 31 9 465\n", " 6: 231 71 26,565\n", " 7: 1,596 670 1,272,810\n", " 8: 5,916 6,614 17,496,570\n", " 9: 40,590 76,215 823,753,755\n", "10: 274,274 920,518 37,612,976,401\n", "11: 1,837,836 11,506,826 1,688,819,662,530\n" ] } ], "source": [ "print(' N: D #pairs(dice) D*(D-1)/2')\n", "for N in ints(2, 11):\n", " dice = list(all_dice(N))\n", " D = len(dice)\n", " print('{:2}: {:9,d} {:12,d} {:17,d}'.format(N, D, len(list(pairs(dice))), D*(D-1)//2))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "collapsed": true, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "OK, we're doing 100,000 times better for *N*=11. But it would still take a long time to test 11 million pairs. Let's just get the answers up to *N*=10:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "button": false, "collapsed": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 26.2 s, sys: 129 ms, total: 26.3 s\n", "Wall time: 26.6 s\n" ] }, { "data": { "text/plain": [ "{2: set(),\n", " 3: set(),\n", " 4: {((1, 2, 2, 3), (1, 3, 3, 5))},\n", " 5: set(),\n", " 6: {((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))},\n", " 7: set(),\n", " 8: {((1, 2, 2, 3, 3, 4, 4, 5), (1, 3, 5, 5, 7, 7, 9, 11)),\n", " ((1, 2, 2, 3, 5, 6, 6, 7), (1, 3, 3, 5, 5, 7, 7, 9)),\n", " ((1, 2, 3, 3, 4, 4, 5, 6), (1, 2, 5, 5, 6, 6, 9, 10))},\n", " 9: {((1, 2, 2, 3, 3, 3, 4, 4, 5), (1, 4, 4, 7, 7, 7, 10, 10, 13))},\n", " 10: {((1, 2, 2, 3, 3, 4, 4, 5, 5, 6), (1, 3, 5, 6, 7, 8, 9, 10, 12, 14))}}" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "{N: sicherman(N)\n", " for N in ints(2, 10)}" ] }, { "cell_type": "markdown", "metadata": { "button": false, "deletable": true, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Not bad; we solved it for all *N* up to 10 in just a few seconds more than it took for just *N*=7 with the old version. Interestingly, there are solutions for all composite *N* but no prime *N*. So I have some questions:\n", "\n", "- Do all composire *N* have solutions?\n", "- Do no prime *N* have solutions?\n", "- Do some *N* have more than the 3 solutions that 8 has?\n", "- Could we handle larger *N* if we worked with [generating functions](https://en.wikipedia.org/wiki/Sicherman_dice)?\n", "\n", "\n", "**Good questions, but I think this is a good place to stop for now.**\n", "\n", "Agreed!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }