Add files via upload
This commit is contained in:
parent
819adb60f8
commit
f77a47a685
@ -8,15 +8,15 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# The Number Bracelets Game\n",
|
"# The Number Bracelets Game\n",
|
||||||
"\n",
|
"\n",
|
||||||
"See Susan Addington's [description of the numbers bracelet game](http://www.geom.uiuc.edu/~addingto/number_bracelets/number_bracelets.html). It says in part:\n",
|
"Susan Addington describes [the numbers bracelet game](http://www.geom.uiuc.edu/~addingto/number_bracelets/number_bracelets.html):\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Imagine that you have lots of beads, numbered from 0 through 9, as many as you want of each kind. Here are the rules for making a number bracelet:\n",
|
"*Imagine that you have lots of beads, numbered from 0 through 9, as many as you want of each kind. Here are the rules for making a number bracelet:*\n",
|
||||||
"\n",
|
"\n",
|
||||||
"- Pick a first and a second bead. They can have the same number.\n",
|
"- *Pick a first and a second bead. They can have the same number.*\n",
|
||||||
"- To get the third bead, add the numbers on the first and second beads. If the sum is more than 9, just use the last (ones) digit of the sum.\n",
|
"- *To get the third bead, add the numbers on the first and second beads. If the sum is more than 9, just use the last (ones) digit of the sum.*\n",
|
||||||
"- To get the next bead, add the numbers on the last two beads you used, and use only the ones digit. So to get the fourth bead, add the numbers on the second and third beads, and use the ones digit.\n",
|
"- *To get the next bead, add the numbers on the last two beads you used, and use only the ones digit. So to get the fourth bead, add the numbers on the second and third beads, and use the ones digit.*\n",
|
||||||
"- Keep going until you get back to the first and second beads, in that order.\n",
|
"- *Keep going until you get back to the first and second beads, in that order.*\n",
|
||||||
"- How long (or short) a bracelet can you make?"
|
"- *Then pop off the last two beads.*"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -26,7 +26,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Making Bracelets\n",
|
"# Making Bracelets\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The function `bracelet` will make a number bracelet, if you give is a pair of starting beads:"
|
"The function `number_bracelet` will make a number bracelet, if you give it a pair of starting beads:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -36,12 +36,12 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def bracelet(beads):\n",
|
"def number_bracelet(beads):\n",
|
||||||
" \"\"\"Given a list of two beads, extend it to be a bracelet, following the rules of the game.\"\"\"\n",
|
" \"\"\"Given a pair of beads, extend the beads until the first two match the last two.\"\"\"\n",
|
||||||
" while len(beads) < 4 or beads[:2] != beads[-2:]:\n",
|
" while True: \n",
|
||||||
" next_bead = (beads[-1] + beads[-2]) % 10\n",
|
" next_bead = (beads[-1] + beads[-2]) % 10\n",
|
||||||
" beads = (*beads, next_bead)\n",
|
" beads = (*beads, next_bead)\n",
|
||||||
" return beads[:-2]"
|
" if beads[:2] == beads[-2:]: return beads[:-2]"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -70,7 +70,15 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"bracelet((2, 6))"
|
"number_bracelet((2, 6))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "4ca0ab11-17ed-43a2-8da4-2863a43ede3c",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -91,7 +99,15 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"bracelet((1, 3))"
|
"number_bracelet((1, 3))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1a7ae42e-fc59-422d-b592-8e7defc04ffd",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -99,28 +115,7 @@
|
|||||||
"id": "72b69698-6e08-4dd9-a725-533a658a175a",
|
"id": "72b69698-6e08-4dd9-a725-533a658a175a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"I'm a bit uncertain about how small a bracelet can be. If the two starting beads are both 0, then all the following numbers will also be 0. Should that result in a bracelet with zero beads (because you drop the last two) or one bead (because you always add at least one before dropping) or two beads (because you add two 0 beads before dropoing)? I decided that the answer should be two, but if you disagree, I'm not going to argue with you."
|
"One question: If the two starting beads are both 0, then all the following beads will also be 0. When do we stop? Do we stop after (0, 0, 0), drop the last two, and end up with a single 0? Or do we keep going to (0, 0, 0, 0), drop the last two, and end up with (0, 0)? I chose the former, but either choice would be ok, I think."
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 4,
|
|
||||||
"id": "f217fc7d-a763-4929-a7dc-3fe7923ee975",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"(0, 0)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 4,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"bracelet((0, 0))"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -130,20 +125,40 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# All Possible Bracelets\n",
|
"# All Possible Bracelets\n",
|
||||||
"\n",
|
"\n",
|
||||||
"For every possible combination of the first two digits, I'll print out the length of the bracelet that can be made from those digits"
|
"There are 100 possible two-digit starting pairs, so there should be 100 possible bracelets. We can make all of them:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "f248d81c-2738-405c-a62e-b127a63a0a42",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"digits = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n",
|
||||||
|
"\n",
|
||||||
|
"all_bracelets = [number_bracelet((first, second)) for first in digits for second in digits]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "ae759911-d85e-4d53-9f2c-a637fb4ee7e9",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"I'll define `show` to print out each bracelet on a line, with the number of beads in the bracelet first:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"id": "881e1520-7120-46ae-95a5-2341444d6cc2",
|
"id": "70773cb8-9359-401a-b79b-165aa12161a6",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
" 2 beads: 00\n",
|
" 1 beads: 0\n",
|
||||||
"60 beads: 011235831459437077415617853819099875279651673033695493257291\n",
|
"60 beads: 011235831459437077415617853819099875279651673033695493257291\n",
|
||||||
"20 beads: 02246066280886404482\n",
|
"20 beads: 02246066280886404482\n",
|
||||||
"60 beads: 033695493257291011235831459437077415617853819099875279651673\n",
|
"60 beads: 033695493257291011235831459437077415617853819099875279651673\n",
|
||||||
@ -247,14 +262,11 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
|
|
||||||
"\n",
|
|
||||||
"def show(bracelets):\n",
|
"def show(bracelets):\n",
|
||||||
" \"\"\"Print each of the bracelets, preceeded by its number of beads.\"\"\"\n",
|
" \"\"\"Print each of the bracelets, preceeded by its number of beads.\n",
|
||||||
" for beads in bracelets:\n",
|
" Squish the beads together with no spaces between them, so they fit on one line.\"\"\"\n",
|
||||||
" print(f'{len(beads):2} beads:', ''.join(map(str, beads)))\n",
|
" for bracelet in bracelets:\n",
|
||||||
"\n",
|
" print(f'{len(bracelet):2} beads: ', *bracelet, sep='')\n",
|
||||||
"all_bracelets = [bracelet((first, second)) for first in digits for second in digits]\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"show(all_bracelets)"
|
"show(all_bracelets)"
|
||||||
]
|
]
|
||||||
@ -266,31 +278,19 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# How Many Bracelets?\n",
|
"# How Many Bracelets?\n",
|
||||||
"\n",
|
"\n",
|
||||||
"I showed 100 bracelets, because there are 100 two-digit starting pairs. But consider these three results:\n",
|
"I have a list of 100 bracelets, but consider the 4-bead bracelet I showed at the top of the page; the one with the list of beads `(2, 6, 8, 4)`. Bracelets are circular, so that's really the same bracelet as `(6, 8, 4, 2)` or `(8, 4, 2, 6)` or `(4, 2, 6, 8)`. So how many *different* bracelets are there?\n",
|
||||||
"- 055\n",
|
|
||||||
"- 505\n",
|
|
||||||
"- 550\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"These are all different as *sequences*, but they are all the same as *circular bracelets*: if you start at the 0 and go around the circle they all are equal to \"055.\" So let's see how many *different* bracelets there are. I'll do that by converting all bracelets into one common form. Out of all the possible starting points on the circle, I'll arbitrarily choose the starting point that would yield the lowest number: 055 is lower than 505 or 550, so 055 will be the common form. Then I'll make a set of all the distinct common forms and show them:"
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"To find out, I'll put all bracelets in a *common form*: I'll choose the sequence that starts with the lowest number in the bracelet, so that would be `(2, 6, 8, 4)`. If a bracelet repeats the lowest number more than once, start with the lowest two-digit pair, and so on. Once I have all the bracelets in common form, make a set out of them and `show` the distinct members of this set:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 6,
|
||||||
"id": "9d1b4cca-cc8c-4268-9c5b-b9d2f2909948",
|
"id": "e3a97290-59bf-44dc-922c-6fbecaa21317",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"(0, 5, 5)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 6,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"def common_form(bracelet):\n",
|
"def common_form(bracelet):\n",
|
||||||
" \"\"\"Represent this bracelet by choosing the lowest numerical rotation out of all possible rotations.\"\"\"\n",
|
" \"\"\"Represent this bracelet by choosing the lowest numerical rotation out of all possible rotations.\"\"\"\n",
|
||||||
@ -298,32 +298,72 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"def rotations(bracelet):\n",
|
"def rotations(bracelet):\n",
|
||||||
" \"\"\"All possible rotations of bracelet.\"\"\"\n",
|
" \"\"\"All possible rotations of bracelet.\"\"\"\n",
|
||||||
" return [bracelet[i:] + bracelet[:i] for i in range(len(bracelet))]\n",
|
" return [bracelet[i:] + bracelet[:i] for i in range(len(bracelet))]"
|
||||||
"\n",
|
|
||||||
"common_form((5, 0, 5))"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 7,
|
||||||
"id": "9ae033fd-062e-4953-9603-4f82da516057",
|
"id": "d8a8e358-b9cf-4bd3-88e2-789145329764",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[(2, 6, 8, 4), (6, 8, 4, 2), (8, 4, 2, 6), (4, 2, 6, 8)]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"rotations((2, 6, 8, 4))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "64f66bdd-f5ff-4861-8861-b33dc2707182",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(2, 6, 8, 4)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"common_form((6, 8, 4, 2))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"id": "0cb6ff68-e15a-4bdf-92e4-2ba63963b8f7",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
" 2 beads: 00\n",
|
|
||||||
" 4 beads: 2684\n",
|
" 4 beads: 2684\n",
|
||||||
" 3 beads: 055\n",
|
" 3 beads: 055\n",
|
||||||
"12 beads: 134718976392\n",
|
"12 beads: 134718976392\n",
|
||||||
"60 beads: 011235831459437077415617853819099875279651673033695493257291\n",
|
"60 beads: 011235831459437077415617853819099875279651673033695493257291\n",
|
||||||
|
" 1 beads: 0\n",
|
||||||
"20 beads: 02246066280886404482\n"
|
"20 beads: 02246066280886404482\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"show({common_form(beads) for beads in all_bracelets})"
|
"show({common_form(b) for b in all_bracelets})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user