{ "cells": [ { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "
Peter Norvig
Jan 2016
revised 2018, 2020
\n", "\n", "# Four 4s, Five 5s, Countdowns, and Equilength Number Names\n", "\n", "On January 1, 2016 Alex Bellos [posed](http://www.theguardian.com/science/2016/jan/04/can-you-solve-it-complete-the-equation-10-9-8-7-6-5-4-3-2-1-2016) this New Year's puzzle:\n", "\n", "\n", "> Fill in the blanks so that this equation makes arithmetical sense:\n", ">\n", "> `10 ␣ 9 ␣ 8 ␣ 7 ␣ 6 ␣ 5 ␣ 4 ␣ 3 ␣ 2 ␣ 1 = 2016`\n", ">\n", "> You are allowed to use *only* the four basic arithmetical operations: +, -, ×, ÷. But brackets (parentheses) can be used wherever needed. So, for example, the solution could begin\n", ">\n", "> `(10 + 9) * (8` ... or `10 + (9 * 8)` ...\n", "\n", "Let's see if we can solve this puzzle, and some of the related ones from Alex's [first](http://www.theguardian.com/science/2016/jan/04/can-you-solve-it-complete-the-equation-10-9-8-7-6-5-4-3-2-1-2016) and [second](http://www.theguardian.com/science/2016/jan/04/did-you-solve-it-complete-the-equation-10-9-8-7-6-5-4-3-2-1-2016) post. First the imports we will eventually need." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from itertools import product\n", "from functools import lru_cache\n", "from fractions import Fraction\n", "from collections import Counter, defaultdict\n", "import re" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# Four Basic Operators, No Brackets\n", "\n", "We'll start with a simpler version of the puzzle: a countdown with no brackets. \n", "\n", "There are nine blanks, each of which can be filled by one of four operators, so there are 49 = 262,144 possibilities, few enough that we can enumerate them all, using `itertools.product` to get tuples of operators, and then `str.format` to plug them into blanks, and then `eval` to evaluate the string:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[('+', '+', '+', '+', '+', '+', '+', '+', '+'),\n", " ('+', '-', '/', '-', '-', '+', '/', '+', '-'),\n", " ('+', '/', '*', '*', '*', '-', '*', '+', '*'),\n", " ('-', '-', '-', '/', '/', '*', '-', '+', '/'),\n", " ('-', '/', '-', '-', '+', '/', '+', '-', '+'),\n", " ('*', '-', '+', '*', '-', '/', '/', '-', '-'),\n", " ('*', '*', '/', '/', '/', '+', '*', '-', '*'),\n", " ('/', '+', '/', '-', '+', '-', '-', '-', '/'),\n", " ('/', '*', '*', '*', '-', '*', '+', '*', '+')]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "allops = list(product(('+', '-', '*', '/'), repeat=9))\n", "\n", "allops[::30001] # Sample a few ops tuples (every 30,001 one)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'10+9+8+7+6+5+4+3+2+1'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'10{}9{}8{}7{}6{}5{}4{}3{}2{}1'.format(*allops[0])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval(_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(*Warning*: don't use eval on strings that you aren't sure are safe from bad effects.)\n", "\n", "We need to catch errors such as dividing by zero, so I'll define a wrapper function, `evaluate`, to do that, and I'll define `simple_countdown` to put the pieces together:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def evaluate(exp):\n", " \"eval(exp), or None if there is an arithmetic error.\"\n", " try:\n", " return eval(exp)\n", " except ArithmeticError:\n", " return None\n", "\n", "def simple_countdown(target, operators=('+', '-', '*', '/')):\n", " \"All solutions to the countdown puzzle (with no brackets).\"\n", " exps = ('10{}9{}8{}7{}6{}5{}4{}3{}2{}1'.format(*ops)\n", " for ops in product(operators, repeat=9))\n", " return [exp for exp in exps if evaluate(exp) == target]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simple_countdown(2016)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Too bad; we did all that work and didn't find a solution. What years *can* we find solutions for? I'll define `simple_countdowns` to take a collection of target years rather than a single one, and return a dict of the form `{year: 'expression'}` for each expression that evaluates to one of the target years. I'll also generalize it to allow any format string, with the default being the countdown string." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{1981: '10*9*8+7*6*5*4*3/2+1',\n", " 1979: '10*9*8+7*6*5*4*3/2-1',\n", " 1980: '10*9*8+7*6*5*4*3/2/1',\n", " 2019: '10*9*8*7/6/5*4*3+2+1',\n", " 2017: '10*9*8*7/6/5*4*3+2-1',\n", " 2018: '10*9*8*7/6/5*4*3+2/1',\n", " 2015: '10*9*8*7/6/5*4*3-2+1',\n", " 2013: '10*9*8*7/6/5*4*3-2-1',\n", " 2014: '10*9*8*7/6/5*4*3-2/1'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def simple_countdowns(targets, operators=('+', '-', '*', '/'), \n", " fmt='10{}9{}8{}7{}6{}5{}4{}3{}2{}1'):\n", " \"All solutions to the countdown puzzle (with no brackets) in target years.\"\n", " exps = (fmt.format(*ops)\n", " for ops in product(operators, repeat=fmt.count('{}')))\n", " return {int(evaluate(exp)): exp \n", " for exp in exps if evaluate(exp) in targets}\n", "\n", "simple_countdowns(range(1900, 2100))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Interesting: in the 20th and 21st centuries, there are only two \"golden eras\" where the bracketless countdown equation works: the three year period centered on 1980, and the seven year period that is centered on 2016, but omits 2016. " ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# Four Basic Operators, With Brackets\n", "\n", "Now we return to the original problem. Can I make use of what I have so far? Well, my second version of `simple_countdown` accepts different format strings, so I could give it all possible bracketed format strings and let it fill in all possible operator combinations. How long would that take? I happen to remember that the number of bracketings of *n* operands is the nth [Catalan number](https://oeis.org/A000108), which I looked up and found to be 4862. A single call to `simple_countdown` took about 2 seconds, so we could do 4862 calls in about 160 minutes. That's not terrible, but (a) it would still take work to generate all the bracketings, and (b) I think I can find another way that is much faster.\n", "\n", "I'll restate the problem as: given the sequence of numbers (10, 9, ... 1), create an expression whose value is 2016. But to get there I'll solve a more general problem: given any sequence of numbers, like `(10, 9, 8)`, what `{value: expression}` pairs can I make with them? I'll define `expressions(numbers)` to return a dict of `{value: expression}` for all expressions (strings) whose numeric value is `value`, and can be made from `numbers`, for example:\n", "\n", " expressions((10,)) ⇒ {10: '10'}\n", " expressions((9, 8)) ⇒ {1: '(9-8)', 1.125: '(9/8)', 17: '(9+8)', 72: '(9*8)'}\n", "\n", "I'll use the idea of [dynamic programming](https://en.wikipedia.org/wiki/Dynamic_programming): break the problem down into simpler subparts, compute an answer for each subpart, and remember intermediate results so we don't need to re-compute them later. How do we break the problem into parts? `expressions((10, 9, 8))` should consist of all the ways of splitting `(10, 9, 8)` into two parts, finding all the expressions that can be made with each part, and combining pairs of expressions with any of the four operators:\n", "\n", " expressions((10, 9, 8)) ⇒ {11: '(10+(9-8))', 27: '(10+(9+8))', 720: '(10*(9*8))', ...}\n", "\n", "First the function `splits`:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "c10 = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)\n", "\n", "def splits(sequence):\n", " \"Split sequence into two non-empty parts, in all ways.\"\n", " return [(sequence[:i], sequence[i:]) \n", " for i in range(1, len(sequence))]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "[((10,), (9, 8, 7, 6, 5, 4, 3, 2, 1)),\n", " ((10, 9), (8, 7, 6, 5, 4, 3, 2, 1)),\n", " ((10, 9, 8), (7, 6, 5, 4, 3, 2, 1)),\n", " ((10, 9, 8, 7), (6, 5, 4, 3, 2, 1)),\n", " ((10, 9, 8, 7, 6), (5, 4, 3, 2, 1)),\n", " ((10, 9, 8, 7, 6, 5), (4, 3, 2, 1)),\n", " ((10, 9, 8, 7, 6, 5, 4), (3, 2, 1)),\n", " ((10, 9, 8, 7, 6, 5, 4, 3), (2, 1)),\n", " ((10, 9, 8, 7, 6, 5, 4, 3, 2), (1,))]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "splits(c10)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Now the function `expressions`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "@lru_cache(None)\n", "def expressions(numbers: tuple) -> dict:\n", " \"Return {value: expr} for all expressions that can be made from numbers.\"\n", " if len(numbers) == 1: \n", " return {numbers[0]: str(numbers[0])}\n", " else: \n", " result = {}\n", " for (Lnums, Rnums) in splits(numbers):\n", " for (L, R) in product(expressions(Lnums), expressions(Rnums)):\n", " Lexp = '(' + expressions(Lnums)[L]\n", " Rexp = expressions(Rnums)[R] + ')'\n", " result[L * R] = Lexp + '*' + Rexp\n", " result[L - R] = Lexp + '-' + Rexp\n", " result[L + R] = Lexp + '+' + Rexp\n", " if R != 0: \n", " result[L / R] = Lexp + '/' + Rexp\n", " return result" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "For example, at one point in a call to `expressions((10, 9, 8))` we would have:\n", "\n", " Lnums, Rnums = (10), (9, 8)\n", " L, R = 10, 1\n", " Lexp, Rexp = '(10', '(9-8))'\n", "\n", "This would lead to us adding the following four entries to `result`:\n", "\n", " result[10] = '(10*(9-8))'\n", " result[9] = '(10-(9-8))'\n", " result[11] = '(10+(9-8))'\n", " result[10] = '(10/(9-8))'\n", " \n", "The decorator `@lru_cache` takes care of storing the intermediate results. Rather than catching errors, we just avoid division by 0.\n", "\n", "Let's give it a try:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{10: '10'}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expressions((10,))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{72: '(9*8)', 1: '(9-8)', 17: '(9+8)', 1.125: '(9/8)'}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expressions((9, 8))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{720: '((10*9)*8)',\n", " -62: '(10-(9*8))',\n", " 82: '((10*9)-8)',\n", " 0.1388888888888889: '((10/9)/8)',\n", " 10: '(10/(9-8))',\n", " 9: '((10-9)+8)',\n", " 11: '((10+9)-8)',\n", " 170: '(10*(9+8))',\n", " -7: '((10-9)-8)',\n", " 27: '((10+9)+8)',\n", " 0.5882352941176471: '(10/(9+8))',\n", " 11.25: '((10*9)/8)',\n", " 8.875: '(10-(9/8))',\n", " 11.125: '(10+(9/8))',\n", " 8.88888888888889: '((10/9)*8)',\n", " 98: '((10*9)+8)',\n", " 8: '((10-9)*8)',\n", " 0.125: '((10-9)/8)',\n", " 152: '((10+9)*8)',\n", " 2.375: '((10+9)/8)',\n", " -6.888888888888889: '((10/9)-8)',\n", " 9.11111111111111: '((10/9)+8)'}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expressions((10, 9, 8))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "That looks reasonable. Let's solve the whole puzzle.\n", "\n", "# Countdown to 2016: A Solution" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 24 s, sys: 617 ms, total: 24.6 s\n", "Wall time: 24.9 s\n" ] }, { "data": { "text/plain": [ "'(((((((((10*9)+8)*7)-6)-5)-4)*3)+2)+1)'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time expressions(c10)[2016]" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "We have an answer! And in seconds, not hours, thanks to dynamic programming! Here are solutions for nearby years:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2010: '((((((10+(((9*8)-7)*6))*5)+4)+3)+2)+1)',\n", " 2011: '((((((10+9)*8)+7)*(6+(5*(4/3))))-2)-1)',\n", " 2012: '((((((10+9)*8)+7)*(6+(5*(4/3))))-2)/1)',\n", " 2013: '((((((10+9)*8)+7)*(6+(5*(4/3))))-2)+1)',\n", " 2014: '(((((((10+((9*8)*7))-6)-5)*4)+3)-2)+1)',\n", " 2015: '(((((((((10*9)+8)*7)-6)-5)-4)*3)+2)/1)',\n", " 2016: '(((((((((10*9)+8)*7)-6)-5)-4)*3)+2)+1)',\n", " 2017: '(((10*(((9*8)-((7*6)/(5+4)))*3))-2)-1)',\n", " 2018: '(((10*(((9*8)-((7*6)/(5+4)))*3))-2)/1)',\n", " 2019: '(((10*(((9*8)-((7*6)/(5+4)))*3))-2)+1)',\n", " 2020: '(((((10+((9+((8+7)*6))*5))*4)+3)-2)-1)',\n", " 2021: '((((((((10-9)+(8*7))*6)-5)*4)*3)/2)-1)',\n", " 2022: '((((((((10-9)+(8*7))*6)-5)*4)*3)/2)/1)',\n", " 2023: '(((((((10*9)*(8+7))/6)*(5+4))-3)+2)-1)',\n", " 2024: '(((((((10*9)*(8+7))/6)*(5+4))-3)+2)/1)',\n", " 2025: '(((((((10*9)*(8+7))/6)*(5+4))-3)+2)+1)'}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{y: expressions(c10)[y] for y in range(2010, 2026)}" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "# Counting Solutions\n", "\n", "Alex Bellos had another challenge: \n", "\n", "> I was half hoping a computer scientist would let me know exactly how many solutions [to the Countdown problem] there are with only the four basic operations. Maybe someone will. \n", "\n", "As it stands, my program can't answer that question, because I only keep one expression for each value. \n", "\n", "Also, I'm not sure what it means to be a distinct solution. For example, are `((10+9)+8)` and `(10+(9+8))` different, or are they same, because they both are equivalent to `(10+9+8)`? Similarly, are `((3-2)-1)` and `(3-(2+1)` different, or the same because they both are equivalent to `(3 + -2 + -1)`? I think the notion of \"distinct solution\" is just inherently ambiguous. My choice is to count each of these as distinct: every expression has exactly ten numbers, nine operators, and nine pairs of brackets, and if an expression differs in any character, it is different. But I won't argue with anyone who prefers a different definition of \"distinct solution.\"\n", "\n", "So how can I count expressions? One approach would be to go back to enumerating every equation (all 4862 × 49 = 1.2 bilion of them) and checking which ones equal 2016. That would take about 40 hours with my Python program. A better approach is to mimic `expressions`, but to make a table of counts, rather than expression strings. I want:\n", "\n", " counts[(10, 9, 8)][27] == 2\n", " \n", "because there are 2 ways to make 27 with the numbers `(10, 9, 8)`, namely, `((10+9)+8)` and `(10+(9+8))`. And in general, if there are `Lcount` ways to make `L` using `Lnums` and `Rcount` ways to make `R` using `Rnums` then there are `Lcount * Rcount` ways of making `L * R` using `Lnums` followed by `Rnums`. So we have:\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "@lru_cache(None)\n", "def counts(numbers: tuple) -> Counter:\n", " \"Return a Counter of {value: count} for every value that can be made from numbers.\"\n", " if len(numbers) == 1: # Only one way to make an expression out of a single number\n", " return Counter(numbers)\n", " else: \n", " result = Counter()\n", " for (Lnums, Rnums) in splits(numbers):\n", " for L in counts(Lnums):\n", " for R in counts(Rnums):\n", " count = counts(Lnums)[L] * counts(Rnums)[R]\n", " result[L + R] += count\n", " result[L - R] += count\n", " result[L * R] += count\n", " if R != 0:\n", " result[L / R] += count\n", " return result" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({4: 2, 0: 1, 1.0: 1})" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "counts((2, 2)) # corresponds to {0: '2-2', 1.0: '2/2', 4: '2+2' or '2*2'}" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "counts((10, 9, 8))[27] # (10+9)+8 or 10+(9+8)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "Looks good to me. Now let's see if we can answer Alex's question." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "30066" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "counts(c10)[2016]" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "This says there are 30,066 distinct expressions for 2016. Is that the answer for Alex?\n", "\n", "# What About Round-off Error?\n", "\n", "Let's find all the values in `counts(c10)` that are very near to `2016`, within ±0.0000000001:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "{2016.0000000000002: 5792,\n", " 2016.0: 30066,\n", " 2015.9999999999995: 1930,\n", " 2015.999999999997: 15,\n", " 2016.0000000000005: 510,\n", " 2016.0000000000018: 264,\n", " 2016.0000000000023: 12,\n", " 2015.9999999999998: 5868,\n", " 2015.9999999999993: 14,\n", " 2016.000000000002: 18,\n", " 2015.999999999999: 10}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{y: counts(c10)[y] \n", " for y in counts(c10)\n", " if abs(y - 2016) < 1e-10}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I suspect that all of these actually should be *exactly* equal to 2016. If they were, then the total count would be:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "44499" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(_.values())" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "To be absolutely sure, I could re-do *all* the calculations using exact rational arithmetic, as provided by the `Fraction` class. From experience I know that would be an order of magnitude slower if used everywhere, so instead I'll just verify the small set of expressions in the output above. The function `exact(exp)` returns a string that will exactly calculate `exp`:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Fraction(1)/(Fraction(2)+Fraction(3))'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def exact(exp): return re.sub(r\"([0-9]+)\", r\"Fraction(\\1)\", exp)\n", "\n", "exact('1/(2+3)')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Fraction(1, 5)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval(_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are all the expressions near 2016:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(Fraction(2016, 1), '((((10*((9*(8+7))-(6/(5+4))))*3)/2)+1)'),\n", " (Fraction(2016, 1), '(((((((((10*9)+8)*7)-6)-5)-4)*3)+2)+1)'),\n", " (Fraction(2016, 1), '((((((10*9)*8)*7)*((6/5)-(4-3)))*2)/1)'),\n", " (Fraction(2016, 1), '(((10*9)*8)/(7-(6-(((5/(4+3))/2)-1))))'),\n", " (Fraction(2016, 1), '(((((10*9)*8)*((((7*6)/5)-4)-3))*2)/1)'),\n", " (Fraction(2016, 1), '((((((10*9)*8)*7)*(((6/5)-4)+3))*2)/1)'),\n", " (Fraction(2016, 1), '(10*((9*8)/((7-(6-((5/(4+3))/2)))-1)))'),\n", " (Fraction(2016, 1), '((10*((9*(((8-(7/6))*5)-(4*3)))+2))+1)'),\n", " (Fraction(2016, 1), '(10*(((((9*8)*7)*6)/5)*(((4/3)-2)+1)))'),\n", " (Fraction(2016, 1), '(((10*9)*8)/((7-(6-((5/(4+3))/2)))-1))'),\n", " (Fraction(2016, 1), '((((10*9)*(8+7))-6)/(((5-(4/3))-2)-1))')]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[(eval(exact(exp)), exp)\n", " for y, exp in expressions(c10).items()\n", " if abs(y - 2016) < 1e-10]" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "They all evaluate to `Fraction(2016, 1)`, which is `2016/1` or just `2016`. This gives me more confidence in the 44,499 count, but I wouldn't accept it as definitive until it was independently verified and passed an extensive test suite. And of course, if you have a different definition of \"distinct solution,\" you will get a different answer; there are multiple possible definitions that are all very reasonable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Four 4s\n", "\n", "Alex Bellos continues with a related puzzle:\n", " \n", "> The most famous “fill in the gaps in the equation” puzzle is known as the [four fours](https://en.wikipedia.org/wiki/Four_fours), because every equation is of the form\n", ">\n", "> `4 ␣ 4 ␣ 4 ␣ 4 = X.`\n", ">\n", ">In the classic form of the puzzle you must find a solution for X = 0 to 9 using just addition, subtraction, multiplication and division (and brackets).\n", "\n", "This puzzle goes back to a [1914 publication](https://archive.org/details/mathematicalrecr00ball) by the \"most famous\" mathematician/magician [W. W. Rouse Ball](https://en.wikipedia.org/wiki/W._W._Rouse_Ball). The solution is easy:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: '(((4-4)-4)+4)',\n", " 1: '(((4/4)-4)+4)',\n", " 2: '((4/(4+4))*4)',\n", " 3: '(((4+4)+4)/4)',\n", " 4: '(((4-4)/4)+4)',\n", " 5: '(((4*4)+4)/4)',\n", " 6: '(((4+4)/4)+4)',\n", " 7: '((4-(4/4))+4)',\n", " 8: '(((4+4)/4)*4)',\n", " 9: '(((4/4)+4)+4)'}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{i: expressions((4, 4, 4, 4))[i] for i in range(10)}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that I didn't do anything special to take advantage of the fact that in `(4, 4, 4, 4)` the digits are all the same. Happily, `lru_cache` does that for me automatically! If I split that into `(4, 4)` and `(4, 4)`, when it comes time to do the second half of the split, the result will already be in the cache, and so won't be recomputed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# New Mathematical Operations\n", "\n", "Bellos then writes:\n", " \n", "> If you want to show off, you can introduce **new mathematical operations** such as powers, square roots, concatenation and decimals, ... or use the factorial symbol, `!`.\n", "\n", "Bellos is suggesting the following operations:\n", "\n", "- **Powers**: `(4 ^ 4)` is 4 to the fourth power, or 256.\n", "- **Square roots:** `√4` is the square root of 4, or 2.\n", "- **Concatenation:** `44` is forty-four.\n", "- **Decimals:** `4.4` is four and four tenths.\n", "- **Factorials** `4!` is 4 × 3 × 2 × 1, or 24.\n", "\n", "There are some complications to deal with:\n", "\n", "- **Irrationals**: `√2` is an irrational number; so we can't do exact rational arithmetic.\n", "- **Imaginaries**: `√-1` is an imaginary number; do we want to deal with that?\n", "- **Overflow**: `(10. ^ (9. ^ 8.))`, as a `float`, gives an `OverflowError`.\n", "- **Out of memory**: [`(10 ^ (9 ^ (8 * 7)))`](http://www.wolframalpha.com/input/?i=10+%5E+9+%5E+56), as an `int`, gives an `OutOfMemoryError` (even if your memory uses every atom on Earth).\n", "- **Infinite expressions**: We could add an infinite number of square root and/or factorial signs to any expression: `√√√√√√(4!!!!!!!!)`...\n", "- **Round-off error**: `(49*(1/49))` evaluates to `0.9999999999999999`, not `1`.\n", "\n", "We could try to manage this with *symbolic algebra*, perhaps using [SymPy](http://www.sympy.org/en/index.html), but that seems complicated, so instead I will make some arbitrary decisions:\n", "\n", "- Use floating point approximations for everything, rational or irrational.\n", "- Disallow irrationals.\n", "- Disallow overflow errors (a type of arithmetic error).\n", "- Put limits on what is allow for exponentiation.\n", "- Allow only two nested unary operations to avoid infinite expressions.\n", "\n", "I'll define the function `do` to do an arithmetic computation, catch any errors, and try to correct round-off errors. The idea is that since my expressions start with integers, any result that is very close to an integer is probably actually that integer. So I'll correct `(49*(1/49))` to be `1.0`. Of course, an expression like `(1+(10^-99))` is also very close to `1.0`, but it should not be rounded off. Instead, I'll try to avoid such expressions by silently dropping any value whose magnitude is outside the range of 10-10 to 1010 (except of course I will accept an exact 0)." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "from operator import add, sub, mul, truediv as div\n", "from math import sqrt, factorial\n", "\n", "def do(op, *args): \n", " \"Return op(*args), trying to correct for roundoff error, or `None` on Error or too big/small.\"\n", " try:\n", " val = op(*args)\n", " return (val if val == 0 else\n", " None if isinstance(val, complex) else\n", " None if not (1e-10 < abs(val) < 1e10) else\n", " round(val) if abs(val - round(val)) < 1e-12 else \n", " val)\n", " except (ArithmeticError, ValueError):\n", " return None" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "assert do(div, 12, 4) == 3 and do(div, 12, 0) == None\n", "assert do(mul, 49, do(div, 1, 49)) == 1\n", "assert do(pow, 10, -99) == None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I'll take this opportunity to refactor `expressions` to have these subfunctions:\n", "- `digit_expressions(result, numbers)`: returns a dict like {0.44: '.44', 4.4: '4.4', 44.0: '44'}\n", "- `add_unary_expressions(result)`: add expressions like `√44` and `4!` to `result` and return `result`. \n", "- `add_binary_expressions(result, numbers)`: add expressions like `4+√44` and `4^4` to `result` and return `result`." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "@lru_cache(None)\n", "def expressions(numbers: tuple) -> dict:\n", " \"Return {value: expr} for all expressions that can be made from numbers.\"\n", " return add_unary_expressions(add_binary_expressions(digit_expressions(numbers), numbers))\n", "\n", "def digit_expressions(digits: tuple) -> dict:\n", " \"All ways of making numbers from these digits (in order), and a decimal point.\"\n", " D = ''.join(map(str, digits))\n", " exps = [(D[:i] + '.' + D[i:]).rstrip('.')\n", " for i in range(len(D) + 1)\n", " if not (D.startswith('0') and i > 1)]\n", " return {float(exp): exp for exp in exps}\n", " \n", "def add_binary_expressions(result: dict, numbers: tuple) -> dict:\n", " \"Add binary expressions by splitting numbers and combining with an op.\"\n", " for (Lnums, Rnums) in splits(numbers):\n", " for (L, R) in product(expressions(Lnums), expressions(Rnums)):\n", " Lexp = '(' + expressions(Lnums)[L]\n", " Rexp = expressions(Rnums)[R] + ')'\n", " assign(result, do(div, L, R), Lexp + '/' + Rexp)\n", " assign(result, do(mul, L, R), Lexp + '*' + Rexp)\n", " assign(result, do(add, L, R), Lexp + '+' + Rexp)\n", " assign(result, do(sub, L, R), Lexp + '-' + Rexp)\n", " if -10 <= R <= 10 and (L > 0 or int(R) == R):\n", " assign(result, do(pow, L, R), Lexp + '^' + Rexp)\n", " return result\n", " \n", "def add_unary_expressions(result: dict, nesting_level=2) -> dict:\n", " \"Add unary expressions: -v, √v and v! to result\"\n", " for _ in range(nesting_level):\n", " for v in tuple(result):\n", " exp = result[v]\n", " if -v not in result:\n", " assign(result, -v, '-' + exp)\n", " if 0 < v <= 100 and 120 * v == round(120 * v): \n", " assign(result, sqrt(v), '√' + exp)\n", " if 3 <= v <= 6 and v == int(v):\n", " assign(result, factorial(v), exp + '!')\n", " return result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function `assign` will silently drop expressions whose value is `None`, and if two expressions have the same value, `assign` keeps the one with the lower \"weight\"—a measure of the characters in the expression. This shows simpler expressions in favor of complex ones: for four 4s, the entry for `0` will be `44-44`, not something like `-√((-√4*--4)*(-√4--√4))`." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "def assign(result: dict, value: float, exp: str): \n", " \"Assign result[value] = exp, unless we already have a lighter exp or value is None.\"\n", " if (value is not None \n", " and (value not in result or weight(exp) < weight(result[value]))): \n", " result[value] = exp\n", " \n", "PENALTIES = defaultdict(int, {'√':7, '!':5, '.':1, '^':3, '/':1, '-':1})\n", " \n", "def weight(exp: str) -> int: return len(exp) + sum(PENALTIES[c] for c in exp)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "I'll define a function to print a table of consecutive integers (starting at 0) that can be made by a sequence of numbers:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [], "source": [ "def show(numbers: tuple, upto=10000, clear=True):\n", " \"\"\"Print expressions for integers from 0 up to limit or the first unmakeable integer.\"\"\"\n", " if clear: expressions.cache_clear() # To free up memory\n", " print('{:,} entries for {}\\n'.format(len(expressions(numbers)), numbers))\n", " for i in range(upto + 1): \n", " if i not in expressions(numbers):\n", " return i\n", " print('{:4} = {}'.format(i, unbracket(expressions(numbers)[i])))\n", " \n", "def unbracket(exp: str) -> str:\n", " \"Strip outer parens from exp, if they are there\"\n", " return (exp[1:-1] if exp.startswith('(') and exp.endswith(')') else exp)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "705,959 entries for (4, 4, 4, 4)\n", "\n", " 0 = 44-44\n", " 1 = 44/44\n", " 2 = 4*(4/(4+4))\n", " 3 = (4+(4+4))/4\n", " 4 = 4+(4*(4-4))\n", " 5 = (4+(4*4))/4\n", " 6 = 4+((4+4)/4)\n", " 7 = (44/4)-4\n", " 8 = 4+(4*(4/4))\n", " 9 = 4+(4+(4/4))\n", " 10 = 44/4.4\n", " 11 = (4/4)+(4/.4)\n", " 12 = (4+44)/4\n", " 13 = 4!-(44/4)\n", " 14 = (4+(.4*4))/.4\n", " 15 = 4+(44/4)\n", " 16 = .4*(44-4)\n", " 17 = (4/4)+(4*4)\n", " 18 = .4+(.4*44)\n", " 19 = (4+(4-.4))/.4\n", " 20 = 4*(4+(4/4))\n", " 21 = (4+4.4)/.4\n", " 22 = √4*(44/4)\n", " 23 = ((4*4!)-4)/4\n", " 24 = 4+(4+(4*4))\n", " 25 = (4+(4*4!))/4\n", " 26 = (4/.4)+(4*4)\n", " 27 = 4+(4!-(4/4))\n", " 28 = 44-(4*4)\n", " 29 = 4+(4/(.4*.4))\n", " 30 = (4+(4+4))/.4\n", " 31 = 4!+((4+4!)/4)\n", " 32 = (4*4)+(4*4)\n", " 33 = 4!+((4-.4)/.4)\n", " 34 = 44-(4/.4)\n", " 35 = 4!+(44/4)\n", " 36 = 44-(4+4)\n", " 37 = ((.4+4!)/.4)-4!\n", " 38 = 44-(4!/4)\n", " 39 = ((4*4)-.4)/.4\n", " 40 = 44-√(4*4)\n", " 41 = (.4+(4*4))/.4\n", " 42 = √4+(44-4)\n", " 43 = 44-(4/4)\n", " 44 = 4+(44-4)\n", " 45 = 44+(4/4)\n", " 46 = 4+(44-√4)\n", " 47 = 4!+(4!-(4/4))\n", " 48 = 4*(4+(4+4))\n", " 49 = 44+(√4/.4)\n", " 50 = (4+(4*4))/.4\n", " 51 = (.4+(4!-4))/.4\n", " 52 = 4+(4+44)\n", " 53 = 4!+(4!+(√4/.4))\n", " 54 = 44+(4/.4)\n", " 55 = 44/(.4+.4)\n", " 56 = 4*(4+(4/.4))\n", " 57 = ((.4+4!)/.4)-4\n", " 58 = ((4^4)-4!)/4\n", " 59 = (4!/.4)-(4/4)\n", " 60 = 44+(4*4)\n", " 61 = (4/4)+(4!/.4)\n", " 62 = (4*(4*4))-√4\n", " 63 = ((4^4)-4)/4\n", " 64 = (4+4)*(4+4)\n", " 65 = (4+(4^4))/4\n", " 66 = √4+(4*(4*4))\n", " 67 = √4+((√4+4!)/.4)\n", " 68 = 4+(4*(4*4))\n", " 69 = (4+(4!-.4))/.4\n", " 70 = (4!+(4^4))/4\n", " 71 = (4!+4.4)/.4\n", " 72 = 4+(4!+44)\n", "CPU times: user 10.2 s, sys: 96.5 ms, total: 10.3 s\n", "Wall time: 10.7 s\n" ] }, { "data": { "text/plain": [ "73" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time show((4, 4, 4, 4))" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "We can also solve the \"2016 with four fours\" puzzle:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "data": { "text/plain": [ "'(4!*(4!+(4!/.4)))'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expressions((4, 4, 4, 4))[2016]" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "In a [separate video](https://www.youtube.com/embed/Noo4lN-vSvw), Alex Bellos shows how to form **every** integer from 0 to infinity using four 4s, if the square root and `log` functions are allowed. The solution comes from Paul Dirac (although [Dirac originally developed it](https://nebusresearch.wordpress.com/2014/04/18/how-dirac-made-every-number/) for the \"four 2s\" problem).\n", "\n", "Donald Knuth has [conjectured](https://www.tandfonline.com/doi/abs/10.1080/0025570X.1964.11975546) that with floor, square root and factorial, you can make any positive integer with just **one** 4.\n", "\n", "Below are some popular variants:\n", "\n", "# Four 2s" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "109,747 entries for (2, 2, 2, 2)\n", "\n", " 0 = 22-22\n", " 1 = 22/22\n", " 2 = 2+(2*(2-2))\n", " 3 = (2+(2*2))/2\n", " 4 = .2*(22-2)\n", " 5 = 2+(2+(2/2))\n", " 6 = 2*(2+(2/2))\n", " 7 = 2+(2/(.2*2))\n", " 8 = 2+(2+(2*2))\n", " 9 = (22/2)-2\n", " 10 = 22/2.2\n", " 11 = (2/2)+(2/.2)\n", " 12 = (2+22)/2\n", " 13 = 2+(22/2)\n", " 14 = 2+(2+(2/.2))\n", " 15 = (2+(2/2))/.2\n", " 16 = 2*(2*(2*2))\n", " 17 = 22-(√.2^-2)\n", " 18 = 22-(2*2)\n", " 19 = (2+(2-.2))/.2\n", " 20 = 22-√(2*2)\n", " 21 = 22-(2/2)\n", " 22 = 2+(22-2)\n", " 23 = 22+(2/2)\n", " 24 = 2*(2+(2/.2))\n", " 25 = 2/(.2*(.2*2))\n", " 26 = 2+(2+22)\n", " 27 = 22+(√.2^-2)\n", " 28 = 2+(2+(2*2)!)\n", " 29 = 2+(2+(.2^-2))\n", " 30 = (2+(2*2))/.2\n" ] }, { "data": { "text/plain": [ "31" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "show((2, 2, 2, 2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Four 9s" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "539,849 entries for (9, 9, 9, 9)\n", "\n", " 0 = 99-99\n", " 1 = 99/99\n", " 2 = (99/9)-9\n", " 3 = (9+(9+9))/9\n", " 4 = 9-(9/(.9+.9))\n", " 5 = √9+((9+9)/9)\n", " 6 = ((9+(9+9))/9)!\n", " 7 = 9-((9+9)/9)\n", " 8 = ((9*9)-9)/9\n", " 9 = 9+(9*(9-9))\n", " 10 = 99/9.9\n", " 11 = 9+((9+9)/9)\n", " 12 = (9+99)/9\n", " 13 = 9+(√9+(9/9))\n", " 14 = 9+(9/(.9+.9))\n", " 15 = 9+((9+9)/√9)\n", " 16 = 9+((9/.9)-√9)\n", " 17 = 9+(9-(9/9))\n", " 18 = 99-(9*9)\n", " 19 = 9+(9+(9/9))\n", " 20 = 9+(99/9)\n", " 21 = (9+9.9)/.9\n", " 22 = 9+(√9+(9/.9))\n", " 23 = √9+((9+9)/.9)\n", " 24 = (99/√9)-9\n", " 25 = 9+(√9!+(9/.9))\n", " 26 = (9*√9)-(9/9)\n", " 27 = 9+(9+√(9*9))\n", " 28 = 9+(9+(9/.9))\n", " 29 = 9+((9+9)/.9)\n", " 30 = (9+(9+9))/.9\n", " 31 = (.9+(9*√9))/.9\n", " 32 = (99-√9)/√9\n", " 33 = √9*(99/9)\n", " 34 = (√9+99)/√9\n", " 35 = (√9!+99)/√9\n", " 36 = 9+(9+(9+9))\n", " 37 = (9/.9)+(9*√9)\n", " 38 = √9!*(√9+(√9/.9))\n", " 39 = 9+(9*(√9/.9))\n", " 40 = (9+(9*√9))/.9\n", " 41 = (.9+(√9!*√9!))/.9\n", " 42 = 9+(99/√9)\n", " 43 = √9+(√9!*(√9!/.9))\n", " 44 = (9*√9!)-(9/.9)\n", " 45 = 9*(9/(.9+.9))\n", " 46 = (9/.9)+(√9!*√9!)\n", " 47 = √9*(9+(√9!/.9))\n", " 48 = √9!*(9-(9/9))\n", " 49 = 9+(√9!*(√9!/.9))\n", " 50 = ((9*√9!)-9)/.9\n", " 51 = 9*(9-(√9/.9))\n", " 52 = √9!*(9-(√9/9))\n", " 53 = (9*√9!)-(9/9)\n", " 54 = 9*((9+9)/√9)\n", " 55 = 99/(.9+.9)\n", " 56 = √9!*(9+(√9/9))\n", " 57 = √9*(9+(9/.9))\n", " 58 = √9!*(9+(√9!/9))\n", " 59 = ((9*√9!)-.9)/.9\n", " 60 = √9*((9+9)/.9)\n", " 61 = (.9+(9*√9!))/.9\n" ] }, { "data": { "text/plain": [ "62" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "show((9, 9, 9, 9))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Four 5s" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "202,937 entries for (5, 5, 5, 5)\n", "\n", " 0 = 55-55\n", " 1 = 55/55\n", " 2 = (5/5)+(5/5)\n", " 3 = (5+(5+5))/5\n", " 4 = ((5*5)-5)/5\n", " 5 = 5+(5*(5-5))\n", " 6 = (55/5)-5\n", " 7 = 5+((5+5)/5)\n", " 8 = 5.5+(.5*5)\n", " 9 = 5+(5-(5/5))\n", " 10 = 55/5.5\n", " 11 = 5.5+5.5\n", " 12 = (5+55)/5\n", " 13 = .5+(.5*(5*5))\n", " 14 = 5+((5-.5)/.5)\n", " 15 = (5*5)-(5+5)\n", " 16 = 5+(55/5)\n", " 17 = 5+(5!/(5+5))\n", " 18 = (5-.5)/(.5*.5)\n", " 19 = (5+(5-.5))/.5\n", " 20 = 5+(5+(5+5))\n", " 21 = (5+5.5)/.5\n", " 22 = 55/(.5*5)\n", " 23 = .5+(5*(5-.5))\n", " 24 = (5*5)-(5/5)\n", " 25 = .5*(55-5)\n", " 26 = (5/5)+(5*5)\n", " 27 = (.5*55)-.5\n", " 28 = .5+(.5*55)\n", " 29 = (5!+(5*5))/5\n", " 30 = 55-(5*5)\n", " 31 = 55-(5!/5)\n", " 32 = ((5+5)/5)^5\n", " 33 = .5*(5!*.55)\n", " 34 = 5+(5+(5!/5))\n", " 35 = 5+(5+(5*5))\n", " 36 = (5!+(.5*5!))/5\n", " 37 = (.5^-5)+√(5*5)\n", " 38 = ((5!/5)-5)/.5\n" ] }, { "data": { "text/plain": [ "39" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "show((5, 5, 5, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Five 5s " ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7,624,387 entries for (5, 5, 5, 5, 5)\n", "\n", " 0 = 5*(55-55)\n", " 1 = 5^(55-55)\n", " 2 = 55/(.5*55)\n", " 3 = .5*((55/5)-5)\n", " 4 = 5-(55/55)\n", " 5 = 5*(55/55)\n", " 6 = 5+(55/55)\n", " 7 = ((5+55)/5)-5\n", " 8 = .5*(5+(55/5))\n", " 9 = (55-(5+5))/5\n", " 10 = (55/5)-(5/5)\n", " 11 = 5*(55/(5*5))\n", " 12 = (5/5)+(55/5)\n", " 13 = (5+(5+55))/5\n", " 14 = (5*5)-(55/5)\n", " 15 = 5+(55/5.5)\n", " 16 = 5+(5.5+5.5)\n", " 17 = 5+((5+55)/5)\n", " 18 = 5.5+(.5*(5*5))\n", " 19 = (5*5)-(.5+5.5)\n", " 20 = 55/(5*.55)\n", " 21 = 5+(5+(55/5))\n", " 22 = (55+55)/5\n", " 23 = (5+(55/.5))/5\n", " 24 = (5+55)/(.5*5)\n", " 25 = 55-(5+(5*5))\n", " 26 = 5*(5+(5/(5*5)))\n", " 27 = 5+(55/(.5*5))\n", " 28 = .5*(.5+55.5)\n", " 29 = 5+((5*5)-(5/5))\n", " 30 = 5*((55/5)-5)\n", " 31 = .5+(5.5+(5*5))\n", " 32 = (5+(55/5))/.5\n", " 33 = .55*(5+55)\n", " 34 = (5!+(55-5))/5\n", " 35 = 5+(55-(5*5))\n", " 36 = (5*5)+(55/5)\n", " 37 = 5+(((5+5)/5)^5)\n", " 38 = .5+(5*(5+(.5*5)))\n", " 39 = ((5*5)-5.5)/.5\n", " 40 = 55-(5+(5+5))\n", " 41 = (5!*.55)-(5*5)\n", " 42 = (5+5.5)/(.5*.5)\n", " 43 = 55-(5!/(5+5))\n", " 44 = 55-(55/5)\n", " 45 = (5*5)+((5*5)-5)\n", " 46 = 55+((.5-5)/.5)\n", " 47 = (5*(5+(5-.5)))-.5\n", " 48 = .5+(5*(5+(5-.5)))\n", " 49 = 55-(.5+5.5)\n", " 50 = 55.5-5.5\n", " 51 = .5+(55.5-5)\n", " 52 = 55-(.5+(.5*5))\n", " 53 = 55-((5+5)/5)\n", " 54 = ((5*55)-5)/5\n", " 55 = .5*(55+55)\n", " 56 = (5+(5*55))/5\n", " 57 = 55+((5+5)/5)\n", " 58 = (.5*5)+55.5\n", " 59 = 5+(55-(5/5))\n", " 60 = 5+(5+(55-5))\n", " 61 = 5.5+55.5\n", " 62 = (.5*(5*(5*5)))-.5\n", " 63 = .5+(.5*(5*(5*5)))\n", " 64 = 55+((5-.5)/.5)\n", " 65 = .5*(5+(5*(5*5)))\n", " 66 = 55+(55/5)\n", " 67 = 55+(5!/(5+5))\n", " 68 = 5.5+(.5*(5+5!))\n", " 69 = 5+(.5^(-.5-5.5))\n", " 70 = 5+(5+(5+55))\n", " 71 = 55+(.5/(.5^5))\n", " 72 = (.5+5.5)!/(5+5)\n", " 73 = (5*5)+(5!/(.5*5))\n", " 74 = 55+((5!/5)-5)\n", " 75 = 55+((5*5)-5)\n", " 76 = 5+(5+(5!*.55))\n", " 77 = 5+(5!*(.5+(.5/5)))\n", " 78 = 55+((5!-5)/5)\n", " 79 = (5!+(5*55))/5\n", " 80 = 5*(5+(55/5))\n", " 81 = 5+(.5*(5!+(.5^-5)))\n", " 82 = 55+((.5^-5)-5)\n", " 83 = (.5*5!)+((5!-5)/5)\n", " 84 = 5+(55+(5!/5))\n", " 85 = 5+(55+(5*5))\n", " 86 = (55/.5)-(5!/5)\n", " 87 = (555-5!)/5\n", " 88 = 5*(.55/(.5^5))\n", " 89 = (5*5)+((.5^-5)/.5)\n", " 90 = (55-(5+5))/.5\n", " 91 = (5*5)+(5!*.55)\n", " 92 = 5+(55+(.5^-5))\n", " 93 = .5+(5!-(.5*55))\n", " 94 = 5!-((5/5)+(5*5))\n", " 95 = (55/.55)-5\n", " 96 = 5!+((5/5)-(5*5))\n", " 97 = 5!+((.5^-5)-55)\n", " 98 = 5!-(55/(.5*5))\n", " 99 = (55-5.5)/.5\n", " 100 = 5*(5+(5+(5+5)))\n", " 101 = (55.5-5)/.5\n", " 102 = (.5+(5*5))/(.5*.5)\n", " 103 = 55+(5!/(.5*5))\n", " 104 = ((55-.5)/.5)-5\n", " 105 = 55+(55-5)\n", " 106 = (555/5)-5\n", " 107 = 5!-(.5+(.5*(5*5)))\n", " 108 = (55-(5/5))/.5\n", " 109 = (55/.5)-(5/5)\n", " 110 = (555-5)/5\n", " 111 = (5/5)+(55/.5)\n", " 112 = (5+555)/5\n", " 113 = .5+(5*(5*(5-.5)))\n", " 114 = 5+((55-.5)/.5)\n", " 115 = 5+(55+55)\n", " 116 = 5+(555/5)\n", " 117 = 5!-((5+(5+5))/5)\n", " 118 = ((5*5!)-(5+5))/5\n", " 119 = 5!-(55/55)\n", " 120 = 5+(5+(55/.5))\n", " 121 = (5+55.5)/.5\n", " 122 = (5*((5*5)-.5))-.5\n", " 123 = .5+(5*((5*5)-.5))\n", " 124 = (5*(5*5))-(5/5)\n", " 125 = .5*(5*(55-5))\n", " 126 = (5/5)+(5*(5*5))\n", " 127 = (5*(.5+(5*5)))-.5\n", " 128 = .5+(5*(.5+(5*5)))\n", " 129 = (5!-55.5)/.5\n", " 130 = (5+(5+55))/.5\n", " 131 = 5!+(5.5+5.5)\n", " 132 = 5!*(.55+.55)\n", " 133 = (5+(5!*5.5))/5\n", " 134 = (5!/5)+(55/.5)\n", " 135 = .5*((5*55)-5)\n", " 136 = 5+(5!+(55/5))\n", " 137 = (.5*(5*55))-.5\n", " 138 = .5+(.5*(5*55))\n", " 139 = ((.5+5.5)!/5)-5\n", " 140 = .5*(5+(5*55))\n", " 141 = 5!+((5+5.5)/.5)\n", " 142 = 5!+(55/(.5*5))\n", " 143 = ((.5+5.5)!-5)/5\n", " 144 = ((55/5)-5)!/5\n", " 145 = (5*(5+(5*5)))-5\n", " 146 = 5!+((5/5)+(5*5))\n", " 147 = 5!+((.5*55)-.5)\n", " 148 = .5+(5!+(.5*55))\n", " 149 = (5!/5)+(5*(5*5))\n", " 150 = 5*(55-(5*5))\n", " 151 = 5!+(55-(5!/5))\n", " 152 = 5!+(((5+5)/5)^5)\n", " 153 = 5!+(.5*(5!*.55))\n", " 154 = 5+(5+(5!+(5!/5)))\n", " 155 = 5+(5*(5+(5*5)))\n", " 156 = (5!+(5!*5.5))/5\n", " 157 = (.5^-5)+(5*(5*5))\n", " 158 = (55+(5!/5))/.5\n", " 159 = (5/(.5^5))-(5/5)\n", " 160 = (55+(5*5))/.5\n", " 161 = (5/5)+(5/(.5^5))\n", " 162 = (5*(.5+(.5^-5)))-.5\n", " 163 = .5+(5*(.5+(.5^-5)))\n", " 164 = 5!+(.5*(5!-(.5^-5)))\n", " 165 = 55+(55/.5)\n", " 166 = 5!+((5!-5)/(.5*5))\n", " 167 = 5!+(((5!/.5)-5)/5)\n", " 168 = (5+(.5*.5))/(.5^5)\n", " 169 = 5!+(((5*5)-.5)/.5)\n", " 170 = 5+(5+(5/(.5^5)))\n", " 171 = (5.5/(.5^5))-5\n", "CPU times: user 2min 24s, sys: 1.17 s, total: 2min 25s\n", "Wall time: 2min 29s\n" ] }, { "data": { "text/plain": [ "172" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time show((5, 5, 5, 5, 5), clear=False)" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false } }, "source": [ "\n", "# Countdown to 2018\n", "\n", "On January 1 2018, [Michael Littman](http://cs.brown.edu/~mlittman/) posted this:\n", "\n", "> 2+0+1×8, 2+0-1+8, (2+0-1)×8, |2-0-1-8|, -2-0+1×8, -(2+0+1-8), sqrt(|2+0-18|), 2+0+1^8, 20-18, 2^(0×18), 2×0×1×8... Happy New Year!\n", "\n", "Can we replicate that countdown? For 2018 and for following years?" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10,303 entries for (2, 0, 1, 8)\n", "\n", " 0 = 2*(0*18)\n", " 1 = 2^(0*18)\n", " 2 = 20-18\n", " 3 = 2.0+(1^8)\n", " 4 = 20*(1-.8)\n", " 5 = -2+((0-1)+8)\n", " 6 = (2*(0-1))+8\n", " 7 = ((2*0)-1)+8\n", " 8 = (2*0)+(1*8)\n", " 9 = (2*0)+(1+8)\n", " 10 = 2.0+(1*8)\n" ] } ], "source": [ "show((2,0,1,8), 10)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14,957 entries for (2, 0, 1, 9)\n", "\n", " 0 = 2*(0*19)\n", " 1 = 20-19\n", " 2 = 2+(0*19)\n", " 3 = 2+(0.1+.9)\n", " 4 = (2*0)+(1+√9)\n", " 5 = 20/(1+√9)\n", " 6 = -2+((0-1)+9)\n", " 7 = (2*(0-1))+9\n", " 8 = ((2*0)-1)+9\n", " 9 = (2*0)+(1*9)\n", " 10 = 20-(1+9)\n" ] } ], "source": [ "show((2,0,1,9), 10)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "373 entries for (2, 0, 2, 0)\n", "\n", " 0 = 202*0\n", " 1 = 20/20\n", " 2 = 2+(0*20)\n", " 3 = 2+(.02^0)\n", " 4 = .20*20\n", " 5 = (2^0)/.20\n", " 6 = (2+(.02^0))!\n" ] }, { "data": { "text/plain": [ "7" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "show((2,0,2,0), 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Uh-oh! I knew 2020 was a bad year! The two zeroes don't give us much to work with, and we can't make all the integers in the countdown. But I can turn a 0 into a 1 with `cos(0)`; maybe that will be enough? Let's try it. I'll modify `add_unary_expressions` to assign values for `sin` and `cos`:" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "from math import sin, cos, pi\n", "\n", "def add_unary_expressions(result: dict, nesting_level=2) -> dict:\n", " \"Add unary expressions: -v, √v and v! to result dict.\"\n", " for _ in range(nesting_level):\n", " for v in tuple(result):\n", " exp = result[v]\n", " if -v not in result:\n", " assign(result, -v, '-' + exp)\n", " if 0 < v <= 100 and 120 * v == round(120 * v): \n", " assign(result, sqrt(v), '√' + exp)\n", " if 3 <= v <= 6 and v == int(v):\n", " assign(result, factorial(v), exp + '!')\n", " if abs(v) in (0, 30, 45, 60, 90, 180, 270, 360):\n", " assign(result, sin(v * pi / 180), 'sin(' + exp + ')')\n", " assign(result, cos(v * pi / 180), 'cos(' + exp + ')')\n", " return result\n", "\n", "PENALTIES.update(s=1, i=1, n=1, c=1, o=1)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8,203 entries for (2, 0, 2, 0)\n", "\n", " 0 = 202*0\n", " 1 = 20/20\n", " 2 = 2+(0*20)\n", " 3 = 2+(.02^0)\n", " 4 = .20*20\n", " 5 = (2^0)/.20\n", " 6 = 2*(cos(0)+2.0)\n", " 7 = 2+(cos(0)/.20)\n", " 8 = 2^(cos(0)+2.0)\n", " 9 = (20/2)-cos(0)\n", " 10 = 2/0.20\n" ] } ], "source": [ "show((2, 0, 2, 0), 10, clear=True) # Clear the cache so we get new results for (2, 0) etc." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "38,975 entries for (2, 0, 2, 1)\n", "\n", " 0 = 2*(0*21)\n", " 1 = -20+21\n", " 2 = 2+(0*21)\n", " 3 = 2+((0*2)+1)\n", " 4 = 2.0*(2*1)\n", " 5 = 2.0+(2+1)\n", " 6 = 2.0*(2+1)\n", " 7 = 2+(0.2^-1)\n", " 8 = 2.0^(2+1)\n", " 9 = (20/2)-1\n", " 10 = 20/(2*1)\n" ] } ], "source": [ "show((2, 0, 2, 1), 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Making 6 from 3 Digits\n", "\n", "Nicolas Schank postedd the following in the Facebook \"omg math\" group.\n", "\n", "> For each digit from 1 to 9, find at least one way to express 6 using only that digit exactly three times and arithmetic operations. For instance, using the digit 2, 2+2^2=6.\n", "\n", "It turns out what I already have handles this well:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: '(1+(1+1))!',\n", " 2: '(2+(2*2))',\n", " 3: '((3*3)-3)',\n", " 4: '((4/.4)-4)',\n", " 5: '(.5+5.5)',\n", " 6: '(6*(6/6))',\n", " 7: '(7-(7/7))',\n", " 8: '√(8+(8/8))!',\n", " 9: '((9+9)/√9)'}" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{n: expressions((n, n, n))[6] for n in range(1, 10)}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# What's Next?\n", "\n", "One exercise would be adding even more operators, such as:\n", "\n", "- **Floor and Ceiling**: `⌊5.5⌋` = 5, `⌈5.5⌉` = 6\n", "- **Nth root**: `3√8` = 2\n", "- **Percent**: `5%` = 5/100\n", "- **Repeating decimal**: `.4...` = .44444444... = 4/9\n", "- **Double factorial**: `9!!` = 9 × 7 × 5 × 3 × 1 = 945; not the same as `(9!)!`\n", "- **Gamma function**: `Γ(n)` = (n − 1)!\n", "- **Prime counting function**: `π(n)` = number of primes ≲ n; `π(5)` = 3\n", "- **Transcendental functions**: besides `sin` and `cos`, there's `log`, `tan`, `arcsin`, ...\n", "\n", "In the [xkcd forum](http://forums.xkcd.com/viewtopic.php?f=14&t=116813&start=280) they got up to 298 for five fives, using the double factorial and π functions. What would you like to do?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "______\n", "\n", "# Equilength Number Names\n", "\n", "The internet [claims](https://www.reddit.com/r/Showerthoughts/comments/3h8wpx/four_is_the_only_number_that_has_the_same_amount/) that \"*Four is the only number that has the same amount of letters as its value.*\" But \"Four\" is not a number; it is a *name* for a number. We'll call it an **equilength number name**.\n", "\n", "Other languages have different equilength number names. In German there's \"vrei\", in Spanish and Portuguese \"cinco\", and in Pinyin Chinese \"èr\" and \"sān\". French does not have an equilength number name.\n", "\n", "There are also *number expressions* such as \"two plus nine\" that are equilength (in letters, not in string length). Let's search for them. I want to define a function similar to `expressions` to generate new expressions and their values. But instead of being required to use a specific set of numbers in creating the expression, I am free to use any numbers in the language. So I will define `equilength_expressions(language, c)` as follows:\n", "\n", "- The input `language` is a pair of components:\n", " - The `operators`, a dict with entries in the form `{add: ['plus', 'and']}`.\n", " - The `integers`, a dict of the form `{(0, 4): 'zero', (1, 3): 'one', ...}`.\n", "- The input `c` tells how many times to `combine` expressions, using the operators of the language. There are an infinite number of possible expressions, so with `c=0` we just get integers like `'one'`; with `c=1` we get a combinations of two integers, like `'(one plus two)`; with `c=2` we get combinations of any previously-defined expression, like `((one plus two) times (four minus three))`. I wouldn't recommend going beyond 2 unless you're willing to wait a long time.\n", "- Like `expressions`, `equilength_expressions` returns a dict such as `{4: 'four'}`.\n", "- However, internally, `equilength_expressions` keeps a `table` with keys that are\n", "`(numeric_value, number_of_letters)` pairs, and values that are string expressions, for example `{(3, 10): \"one plus two\", (11, 11): '(two plus nine)'}`. The dict only keeps one entry for each such key; it would be unneccessary to report that both `'two plus nine'` and `'nine plus two'` are equilength expressions. \n", "- We call `combine` to make larger expressions; it generates all combinations that don't divide by zero. If we allowed operations like `\"factorial\"` and `\"to the power of\"` then we would have to be more careful. \n", "\n" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "def make_key(value, exp): return (value, sum(ch.isalpha() for ch in exp))\n", "def make_item(value, exp): return (make_key(value, exp), exp)\n", "\n", "def names(text): return [word.replace('-', ' ') for word in text.split()]\n", "\n", "def language(operators, integers):\n", " \"\"\"A language is a pair such as: ({add: ['add'],...}, {(0, 4): 'zero',...}).\"\"\"\n", " return ({op: names(operators[op]) for op in operators},\n", " {make_key(i, w): w for i, w in enumerate(names(integers))})" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "def equilength_expressions(language, c=1):\n", " \"\"\"Combine expressions in the language with each other (using operators) c times.\n", " Then report all {n: expression} entries where expression has n letters.\"\"\"\n", " operators, table = language\n", " for _ in range(c):\n", " table = {**table, **combine(operators, table)}\n", " return {n: table[n, v] \n", " for n, v in sorted(table) \n", " if n == v}\n", " \n", "def combine(operators, table):\n", " \"\"\"Return table like {(3, 10): \"(one plus two)\"} by combining table entries with operators.\"\"\"\n", " return dict(make_item(do(OP, L, R), f'({Lexp} {OPexp} {Rexp})')\n", " for (R, _), Rexp in table.items()\n", " for OP in operators\n", " if not (OP == div and R == 0)\n", " for (L, _), Lexp in table.items()\n", " for OPexp in operators[OP])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the names for operators and the first few integers in English, Spanish, Pinyin Chinese and German:" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "english = language({add: 'plus and added-to', sub: 'minus less take-away', \n", " mul: 'times multiplied-by', div: 'divided-by over',\n", " lambda x, y: y-x: 'subtracted-from'},\n", " '''zero one two three four five six seven eight nine ten eleven twelve thirteen \n", " fourteen fifteen sixteen seventeen eighteen nineteen twenty''')\n", "\n", "spanish = language({add: 'más', sub: 'menos', mul: 'por', div: 'dividido-entre dividido-por'},\n", " '''cero uno dos tres cuatro cinco seis siete ocho nueve diez once doce trece catorce \n", " quince dieciséis diecisiete dieciocho diecinueve veinte veintiuno veintidós veintitrés \n", " veinticuatro veinticinco veintiséis veintisiete veintiocho veintinueve treinta''')\n", "\n", "chinese = language({add: 'jiā', sub: 'jiǎn', mul: 'chéng', div: 'chú chú-yǐ'},\n", " 'ling yī èr sān sì wŭ liù qī bā jiŭ shí shí-yī shí-èr shí-sān shí-sì shí-wǔ shí-liù shí-qī')\n", "\n", "german = language({add: 'und plus', sub: 'weniger minus', mul: 'mal multipliziert', div: 'durch'},\n", " '''null eins zwei drei vier fünf sechs sieben acht neun zehn elf zwölf dreizehn \n", " vierzehn funfzehn sechszehn siebzehn achtzehn neunzehn zwanzig einundzwanzig zweiundzwanzig \n", " dreiundzwanzig vierundzwanzig fünfundzwanzig sechsundzwanzig siebenundzwanzig achtundzwanzig''')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Multilingual Equilength Expressions\n", "\n", "We are ready to find equilength numbers and expressions:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{4: 'four'}" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equilength_expressions(english, 0)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{4: 'four',\n", " 10: '(zero and ten)',\n", " 11: '(two plus nine)',\n", " 12: '(one and eleven)',\n", " 13: '(one plus twelve)',\n", " 14: '(one and thirteen)',\n", " 15: '(one times fifteen)',\n", " 16: '(three and thirteen)',\n", " 17: '(one times seventeen)',\n", " 18: '(seven added to eleven)',\n", " 19: '(zero added to nineteen)',\n", " 20: '(seven added to thirteen)'}" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equilength_expressions(english, 1)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{5: 'cinco',\n", " 10: '(uno por diez)',\n", " 11: '(cero más once)',\n", " 12: '(tres más nueve)',\n", " 14: '(cero más catorce)',\n", " 15: '(quince menos cero)',\n", " 16: '(cero más dieciséis)',\n", " 17: '(cero más diecisiete)',\n", " 18: '(veintiuno menos tres)',\n", " 19: '(diecinueve menos cero)',\n", " 20: '(veinte dividido por uno)',\n", " 21: '(veinticuatro menos tres)',\n", " 25: '(veinticinco dividido por uno)',\n", " 26: '(veintiséis dividido entre uno)',\n", " 27: '(veintisiete dividido entre uno)'}" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equilength_expressions(spanish, 1)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2: 'èr',\n", " 3: 'sān',\n", " 7: '(èr jiā wŭ)',\n", " 8: '(èr jiā liù)',\n", " 9: '(sān jiā liù)',\n", " 10: '(yī chéng shí)',\n", " 11: '(shí wǔ jiǎn sì)',\n", " 12: '(yī chéng shí èr)',\n", " 13: '(yī chéng shí sān)'}" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equilength_expressions(chinese, 1)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{4: 'vier',\n", " 11: '(null plus elf)',\n", " 12: '(eins mal zwölf)',\n", " 13: '(eins plus zwölf)',\n", " 15: '(eins mal funfzehn)',\n", " 16: '(eins mal sechszehn)',\n", " 17: '(eins plus sechszehn)',\n", " 18: '(zwanzig weniger zwei)',\n", " 19: '(neunzehn weniger null)',\n", " 21: '(null plus einundzwanzig)',\n", " 22: '(null plus zweiundzwanzig)',\n", " 23: '(achtundzwanzig minus fünf)',\n", " 24: '(sechsundzwanzig minus zwei)',\n", " 25: '(achtundzwanzig weniger drei)',\n", " 26: '(sechsundzwanzig weniger null)',\n", " 27: '(siebenundzwanzig weniger null)'}" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equilength_expressions(german, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Longer Multilingual Equilength Expressions¶\n", "\n", "Now for expressions with 2 combination steps. These take a few minutes each, so I will `%time` them:" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3min 46s, sys: 1.34 s, total: 3min 48s\n", "Wall time: 3min 52s\n" ] }, { "data": { "text/plain": [ "{4: 'four',\n", " 10: '(zero and ten)',\n", " 11: '(two plus nine)',\n", " 12: '(one and eleven)',\n", " 13: '(one plus twelve)',\n", " 14: '(one and thirteen)',\n", " 15: '(one times fifteen)',\n", " 16: '(zero and (six and ten))',\n", " 17: '(one plus (six plus ten))',\n", " 18: '(ten minus (two less ten))',\n", " 19: '(ten minus (one minus ten))',\n", " 20: '(six over (six over twenty))',\n", " 21: '(two minus (one less twenty))',\n", " 22: '(eight less (six less twenty))',\n", " 23: '(seven less (four less twenty))',\n", " 24: '(ten take away (six less twenty))',\n", " 25: '(six take away (one minus twenty))',\n", " 26: '((two and ten) less (six less twenty))',\n", " 27: '((one and ten) less (four less twenty))',\n", " 28: '((one plus six) over (five over twenty))',\n", " 29: '((five plus ten) minus (six less twenty))',\n", " 30: '(ten and (zero subtracted from twenty))',\n", " 31: '((one and sixteen) minus (six less twenty))',\n", " 32: '(two times (four subtracted from twenty))',\n", " 33: '((zero and nineteen) minus (six less twenty))',\n", " 34: '((seventeen over two) over (five over twenty))',\n", " 35: '(nineteen and (four subtracted from twenty))',\n", " 36: '((ten and ten) and (four subtracted from twenty))',\n", " 37: '(seventeen plus (zero subtracted from twenty))',\n", " 38: '((eight and ten) and (zero subtracted from twenty))',\n", " 39: '((nine plus ten) plus (zero subtracted from twenty))',\n", " 40: '((ten over four) times (four subtracted from twenty))',\n", " 41: '((five and twenty) plus (four subtracted from twenty))',\n", " 42: '((ten plus sixteen) plus (four subtracted from twenty))',\n", " 43: '((eight and nineteen) and (four subtracted from twenty))',\n", " 44: '((five times twelve) minus (four subtracted from twenty))',\n", " 45: '((ten added to nineteen) and (four subtracted from twenty))',\n", " 46: '((eleven plus nineteen) plus (four subtracted from twenty))',\n", " 47: '((thirteen plus eighteen) and (four subtracted from twenty))',\n", " 48: '((twenty less seventeen) times (four subtracted from twenty))',\n", " 49: '((five times thirteen) take away (four subtracted from twenty))',\n", " 50: '((six multiplied by eleven) minus (four subtracted from twenty))',\n", " 51: '((seventeen added to eighteen) and (four subtracted from twenty))',\n", " 52: '((thirteen over four) multiplied by (four subtracted from twenty))',\n", " 53: '((seventeen added to twenty) added to (four subtracted from twenty))',\n", " 54: '((two multiplied by nineteen) added to (four subtracted from twenty))',\n", " 55: '((eleven divided by four) multiplied by (zero subtracted from twenty))',\n", " 56: '((four multiplied by eighteen) take away (four subtracted from twenty))',\n", " 57: '((five multiplied by twelve) multiplied by (nineteen divided by twenty))',\n", " 58: '((twelve added to seventeen) times (seventeen subtracted from nineteen))',\n", " 59: '((three multiplied by nineteen) and (seventeen subtracted from nineteen))',\n", " 60: '((fourteen subtracted from seventeen) times (zero subtracted from twenty))',\n", " 61: '((five multiplied by thirteen) take away (fifteen subtracted from nineteen))',\n", " 62: '((eleven added to twenty) multiplied by (seventeen subtracted from nineteen))',\n", " 63: '((five multiplied by thirteen) take away (seventeen subtracted from nineteen))',\n", " 64: '((eight multiplied by sixteen) divided by (seventeen subtracted from nineteen))',\n", " 65: '((thirteen multiplied by twenty) divided by (fourteen subtracted from eighteen))',\n", " 66: '((fourteen added to nineteen) multiplied by (seventeen subtracted from nineteen))',\n", " 67: '((nine multiplied by nineteen) subtracted from (fourteen multiplied by seventeen))',\n", " 68: '((seventeen added to seventeen) multiplied by (seventeen subtracted from nineteen))',\n", " 69: '((seventeen subtracted from eighteen) subtracted from (five multiplied by fourteen))',\n", " 70: '((three subtracted from seventeen) multiplied by (fourteen subtracted from nineteen))',\n", " 71: '((fourteen multiplied by eighteen) subtracted from (seventeen multiplied by nineteen))',\n", " 72: '((seventeen multiplied by seventeen) subtracted from (nineteen multiplied by nineteen))'}" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time equilength_expressions(english, 2)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4min 50s, sys: 3.14 s, total: 4min 53s\n", "Wall time: 5min 5s\n" ] }, { "data": { "text/plain": [ "{5: 'cinco',\n", " 10: '(uno por diez)',\n", " 11: '(cero más once)',\n", " 12: '(tres más nueve)',\n", " 14: '(cero más catorce)',\n", " 15: '(quince menos cero)',\n", " 16: '(uno por (dos por ocho))',\n", " 17: '(uno por (seis más once))',\n", " 18: '(uno por (siete más once))',\n", " 19: '(cero más (seis más trece))',\n", " 20: '(diez por (doce menos diez))',\n", " 21: '(diez menos (uno menos doce))',\n", " 22: '(diez menos (uno menos trece))',\n", " 23: '(diez menos (dos menos quince))',\n", " 24: '(dos menos (ocho menos treinta))',\n", " 25: '(seis menos (once menos treinta))',\n", " 26: '(siete menos (once menos treinta))',\n", " 27: '(cero menos (uno menos veintiocho))',\n", " 28: '(dos menos (tres menos veintinueve))',\n", " 29: '((uno por uno) menos (dos menos treinta))',\n", " 30: '((seis por ocho) más (doce menos treinta))',\n", " 31: '((uno más doce) menos (doce menos treinta))',\n", " 32: '((uno más trece) menos (doce menos treinta))',\n", " 33: '(veinte menos (diecisiete menos treinta))',\n", " 34: '(treinta menos (veintiséis menos treinta))',\n", " 35: '((tres más catorce) menos (doce menos treinta))',\n", " 36: '((dos menos seis) por (veintiuno menos treinta))',\n", " 37: '((tres por trece) más (veintiocho menos treinta))',\n", " 38: '((tres por doce) menos (veintiocho menos treinta))',\n", " 39: '((seis por quince) por (trece dividido por treinta))',\n", " 40: '(doce dividido por (nueve dividido entre treinta))',\n", " 41: '((quince más treinta) más (veintiséis menos treinta))',\n", " 42: '((cinco por doce) por (veintiuno dividido por treinta))',\n", " 43: '((seis más veintiocho) menos (veintiuno menos treinta))',\n", " 44: '((cinco por doce) por (veintidós dividido entre treinta))',\n", " 45: '(veintiuno dividido por (catorce dividido por treinta))',\n", " 46: '((cuatro por quince) por (veintitrés dividido por treinta))',\n", " 47: '((veinte más veinticinco) menos (veintiocho menos treinta))',\n", " 48: '((dos por treinta) por (veinticuatro dividido entre treinta))',\n", " 49: '((veintiuno más veintiséis) menos (veintiocho menos treinta))',\n", " 50: '((dos por quince) dividido por (dieciocho dividido por treinta))',\n", " 51: '((veinticuatro más veintinueve) más (veintiocho menos treinta))',\n", " 52: '((veinticuatro más veintiséis) menos (veintiocho menos treinta))',\n", " 53: '((veinticuatro más veintisiete) menos (veintiocho menos treinta))',\n", " 54: '((veintiuno menos doce) dividido entre (cinco dividido por treinta))',\n", " 55: '((diez más veintitrés) dividido por (dieciocho dividido por treinta))',\n", " 56: '((veintidós más veintinueve) menos (veinticuatro menos veintinueve))',\n", " 57: '((diecinueve dividido por diez) dividido por (uno dividido por treinta))',\n", " 58: '((seis por nueve) dividido entre (veintisiete dividido por veintinueve))',\n", " 59: '((veintinueve dividido entre veintinueve) por (veintinueve más treinta))',\n", " 60: '((veintiocho más treinta) dividido por (veintinueve dividido por treinta))',\n", " 62: '((cuatro más veintisiete) dividido entre (doce dividido entre veinticuatro))',\n", " 63: '((veintiuno dividido entre dos) dividido entre (cinco dividido entre treinta))',\n", " 64: '((veintiuno más veintisiete) dividido por (veintiuno dividido por veintiocho))',\n", " 65: '((treinta más treinta) dividido entre (veinticuatro dividido entre veintiséis))',\n", " 66: '((diecisiete más veintisiete) dividido por (dieciocho dividido por veintisiete))',\n", " 68: '((veintidós más veintinueve) dividido entre (veintiuno dividido entre veintiocho))',\n", " 69: '((veintiuno más veinticinco) dividido entre (dieciocho dividido entre veintisiete))',\n", " 70: '((veintisiete más veintinueve) dividido entre (veinticuatro dividido entre treinta))',\n", " 72: '((veintiocho más veintinueve) dividido entre (diecinueve dividido entre veinticuatro))'}" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time equilength_expressions(spanish, 2)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 20.2 s, sys: 286 ms, total: 20.5 s\n", "Wall time: 21.1 s\n" ] }, { "data": { "text/plain": [ "{2: 'èr',\n", " 3: 'sān',\n", " 7: '(èr jiā wŭ)',\n", " 8: '(èr jiā liù)',\n", " 9: '(sān jiā liù)',\n", " 10: '(yī chéng shí)',\n", " 11: '(shí wǔ jiǎn sì)',\n", " 12: '(yī jiā (sì jiā qī))',\n", " 13: '(yī jiā (èr jiā shí))',\n", " 14: '(qī jiǎn (yī jiǎn bā))',\n", " 15: '(bā chú (bā chú shí wǔ))',\n", " 16: '(qī chú (qī chú shí liù))',\n", " 17: '(shí chú (shí chú shí qī))',\n", " 18: '(sān jiǎn (èr jiǎn shí qī))',\n", " 19: '(èr jiǎn (ling jiǎn shí qī))',\n", " 20: '(sān jiǎn (ling jiǎn shí qī))',\n", " 21: '(shí sì jiǎn (shí jiǎn shí qī))',\n", " 22: '((èr jiā wŭ) jiǎn (èr jiǎn shí qī))',\n", " 23: '(shí qī jiǎn (shí yī jiǎn shí qī))',\n", " 24: '((qī jiā shí) jiǎn (shí jiǎn shí qī))',\n", " 25: '((jiŭ jiā jiŭ) jiǎn (shí jiǎn shí qī))',\n", " 26: '((sì chéng bā) jiā (shí yī jiǎn shí qī))',\n", " 27: '((sān jiā shí qī) jiǎn (shí jiǎn shí qī))',\n", " 28: '((bā jiā shí wǔ) jiǎn (shí èr jiǎn shí qī))',\n", " 29: '((bā jiā shí liù) jiǎn (shí èr jiǎn shí qī))',\n", " 30: '((èr chéng shí qī) chéng (shí wǔ chú shí qī))',\n", " 31: '((shí yī jiā shí wǔ) jiǎn (shí èr jiǎn shí qī))',\n", " 32: '((shí qī jiā shí qī) chéng (shí liù chú shí qī))',\n", " 33: '((sān chéng shí qī) chéng (shí yī chú yǐ shí qī))',\n", " 34: '((shí liù jiā shí liù) chú (shí liù chú yǐ shí qī))',\n", " 35: '((shí liù jiā shí liù) jiǎn (shí sān jiǎn shí liù))',\n", " 36: '((shí èr chéng shí liù) jiǎn (shí èr chéng shí sān))'}" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time equilength_expressions(chinese, 2)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 5min 7s, sys: 4.02 s, total: 5min 11s\n", "Wall time: 5min 20s\n" ] }, { "data": { "text/plain": [ "{4: 'vier',\n", " 11: '(null plus elf)',\n", " 12: '(eins mal zwölf)',\n", " 13: '(eins plus zwölf)',\n", " 15: '(eins mal funfzehn)',\n", " 16: '(eins mal sechszehn)',\n", " 17: '(eins und (fünf und elf))',\n", " 18: '(eins und (sechs und elf))',\n", " 19: '(eins mal (neun plus zehn))',\n", " 20: '(elf minus (zwei minus elf))',\n", " 21: '(zehn minus (null minus elf))',\n", " 22: '(sechs durch (drei durch elf))',\n", " 23: '((elf durch elf) und (elf und elf))',\n", " 24: '(sechs durch (drei durch zwölf))',\n", " 25: '(zehn durch (acht durch zwanzig))',\n", " 26: '(elf weniger (fünf minus zwanzig))',\n", " 27: '(zehn weniger (drei minus zwanzig))',\n", " 28: '(zwölf weniger (vier minus zwanzig))',\n", " 29: '(vierzehn minus (fünf minus zwanzig))',\n", " 30: '(elf minus (zwei minus einundzwanzig))',\n", " 31: '(elf minus (acht minus achtundzwanzig))',\n", " 32: '(fünf minus (eins minus achtundzwanzig))',\n", " 33: '(sechs minus (eins minus achtundzwanzig))',\n", " 34: '(acht weniger (zwei minus achtundzwanzig))',\n", " 35: '(elf minus (drei weniger siebenundzwanzig))',\n", " 36: '(acht weniger (null weniger achtundzwanzig))',\n", " 37: '((acht mal acht) und (eins minus achtundzwanzig))',\n", " 38: '((eins und elf) minus (zwei minus achtundzwanzig))',\n", " 39: '((zwei plus elf) minus (zwei minus achtundzwanzig))',\n", " 40: '(neunzehn minus (sieben weniger achtundzwanzig))',\n", " 41: '(zwanzig weniger (sieben weniger achtundzwanzig))',\n", " 42: '((zehn und elf) durch (vierzehn durch achtundzwanzig))',\n", " 43: '((drei mal zehn) minus (funfzehn minus achtundzwanzig))',\n", " 44: '((zehn und zwölf) durch (vierzehn durch achtundzwanzig))',\n", " 45: '((fünf mal vierzehn) mal (achtzehn durch achtundzwanzig))',\n", " 46: '((drei und zwanzig) durch (vierzehn durch achtundzwanzig))',\n", " 47: '((sechs mal neun) und (einundzwanzig minus achtundzwanzig))',\n", " 48: '((vier mal neun) durch (einundzwanzig durch achtundzwanzig))',\n", " 49: '((neun plus neunzehn) durch (sechszehn durch achtundzwanzig))',\n", " 50: '((sieben plus achtzehn) durch (vierzehn durch achtundzwanzig))',\n", " 51: '((drei mal achtundzwanzig) mal (siebzehn durch achtundzwanzig))',\n", " 52: '((vier mal vierzehn) mal (sechsundzwanzig durch achtundzwanzig))',\n", " 53: '((sechs mal neun) plus (siebenundzwanzig weniger achtundzwanzig))',\n", " 54: '((sieben multipliziert zwölf) mal (achtzehn durch achtundzwanzig))',\n", " 55: '((vier mal vierzehn) und (siebenundzwanzig weniger achtundzwanzig))',\n", " 56: '((zwölf plus sechsundzwanzig) durch (neunzehn durch achtundzwanzig))',\n", " 57: '((sieben mal acht) weniger (siebenundzwanzig weniger achtundzwanzig))',\n", " 58: '((acht multipliziert acht) plus (zweiundzwanzig minus achtundzwanzig))',\n", " 59: '((sieben multipliziert neun) und (vierundzwanzig minus achtundzwanzig))',\n", " 60: '((zwanzig multipliziert achtundzwanzig) mal (drei durch achtundzwanzig))',\n", " 61: '((fünf multipliziert dreizehn) und (vierundzwanzig minus achtundzwanzig))',\n", " 62: '((vier multipliziert siebzehn) plus (zweiundzwanzig minus achtundzwanzig))',\n", " 63: '((drei multipliziert achtzehn) durch (vierundzwanzig durch achtundzwanzig))',\n", " 64: '((drei mal einundzwanzig) weniger (siebenundzwanzig weniger achtundzwanzig))',\n", " 65: '((zehn multipliziert dreizehn) multipliziert (vierzehn durch achtundzwanzig))',\n", " 66: '((vier multipliziert einundzwanzig) mal (zweiundzwanzig durch achtundzwanzig))',\n", " 67: '((siebenundzwanzig und siebenundzwanzig) minus (funfzehn minus achtundzwanzig))',\n", " 68: '((dreiundzwanzig und achtundzwanzig) durch (einundzwanzig durch achtundzwanzig))',\n", " 69: '((vier multipliziert siebzehn) weniger (siebenundzwanzig weniger achtundzwanzig))',\n", " 70: '((elf weniger einundzwanzig) multipliziert (einundzwanzig weniger achtundzwanzig))',\n", " 71: '((drei multipliziert vierundzwanzig) und (siebenundzwanzig weniger achtundzwanzig))',\n", " 72: '((sechs multipliziert vierzehn) multipliziert (vierundzwanzig durch achtundzwanzig))',\n", " 73: '((drei multipliziert vierundzwanzig) minus (siebenundzwanzig weniger achtundzwanzig))',\n", " 74: '((drei multipliziert fünfundzwanzig) minus (siebenundzwanzig weniger sechsundzwanzig))',\n", " 75: '((dreiundzwanzig minus achtundzwanzig) multipliziert (dreizehn weniger achtundzwanzig))',\n", " 76: '((drei multipliziert fünfundzwanzig) weniger (sechsundzwanzig weniger siebenundzwanzig))',\n", " 77: '((sechszehn weniger siebenundzwanzig) multipliziert (einundzwanzig minus achtundzwanzig))',\n", " 78: '((drei multipliziert achtundzwanzig) multipliziert (sechsundzwanzig durch achtundzwanzig))',\n", " 79: '((fünfundzwanzig mal achtundzwanzig) minus (dreiundzwanzig multipliziert siebenundzwanzig))',\n", " 80: '((sechsundzwanzig mal achtundzwanzig) minus (vierundzwanzig multipliziert siebenundzwanzig))',\n", " 81: '((drei multipliziert sechsundzwanzig) multipliziert (siebenundzwanzig durch sechsundzwanzig))',\n", " 82: '((achtundzwanzig mal achtundzwanzig) weniger (sechsundzwanzig multipliziert siebenundzwanzig))',\n", " 83: '((einundzwanzig multipliziert fünfundzwanzig) weniger (siebzehn multipliziert sechsundzwanzig))',\n", " 84: '((einundzwanzig multipliziert fünfundzwanzig) minus (einundzwanzig multipliziert einundzwanzig))',\n", " 86: '((zweiundzwanzig multipliziert sechsundzwanzig) weniger (achtzehn multipliziert siebenundzwanzig))',\n", " 87: '((zweiundzwanzig multipliziert achtundzwanzig) minus (dreiundzwanzig multipliziert dreiundzwanzig))',\n", " 88: '((zweiundzwanzig multipliziert sechsundzwanzig) minus (zweiundzwanzig multipliziert zweiundzwanzig))',\n", " 89: '((zweiundzwanzig multipliziert sechsundzwanzig) weniger (einundzwanzig multipliziert dreiundzwanzig))',\n", " 90: '((zweiundzwanzig multipliziert siebenundzwanzig) weniger (einundzwanzig multipliziert vierundzwanzig))'}" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time equilength_expressions(german, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# What's Next?\n", "\n", "As with the other problems, there are many ways we could extend this: adding more languages, more integers, more operators, etc. It is up to you what to explore next." ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }