Skip to content

Instantly share code, notes, and snippets.

@catawbasam
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catawbasam/81b7a15f6de004540eaa to your computer and use it in GitHub Desktop.
Save catawbasam/81b7a15f6de004540eaa to your computer and use it in GitHub Desktop.
Julia24.ipynb
{
"metadata": {
"language": "Julia",
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# '24' Game Solver Computer Program\n",
"Computers are dumb, but they are fast and accurate at arithmetic, so we will find our solutions using brute force. That means we're going to check every possible combination of numbers and functions.\n",
"\n",
"We program a computer, by writing a series of instructions in a computer language. Our program is written in a language named 'Julia'. Julia is especially good at math."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## About Computer Programs: Data, Algorithms and Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When we program a computer, we are teaching it how to do something.\n",
"Computer programs work mostly with data, algorithms, and functions. \n",
"\n",
"In the 24 game our **data** is an array of four numbers, for example [2,4,6,8]. \n",
"\n",
"**Algorithms** define a series of steps to solve a problem. You learn algorithms too, for example the procedure for multi-digit addition.\n",
"\n",
"A **function** is a verb. It accepts some data, does something to it, and returns some other data. Addition, subtraction, multiplication and division are basic functions you already know about. Computer languages include many other functions, and allow us to define new functions too."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"3+5"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 55,
"text": [
"8"
]
}
],
"prompt_number": 55
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"111111 * 999999 # computers can calculate big numbers easily"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 56,
"text": [
"111110888889"
]
}
],
"prompt_number": 56
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Permutation Function: Permutations are different orders.\n",
"some example permutations of [2,4,6,8]:\n",
"\n",
"* [2,4,6,8] \n",
"* [4,2,6,8] \n",
"* [6,2,4,8] \n",
"* [8,2,4,6]\n",
"\n",
"#### There are 24 possible permutations of our four numbers. Why?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Julia has built-in functions for calculating the number of permutations and for finding permutions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"factorial(4) # how many permutations of 4 items are there?"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 57,
"text": [
"24"
]
}
],
"prompt_number": 57
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nthperm([2,4,6,8],4)' # what is the fourth permutation?"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 58,
"text": [
"1x4 Array{Int64,2}:\n",
" 2 6 8 4"
]
}
],
"prompt_number": 58
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Let's list all the permutations \n",
"computers are really good at repetitive tasks!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"numbers = [2,4,6,8]\n",
"i=1\n",
"for i in 1:24\n",
" print( nthperm(numbers, i)' )\n",
" i = i+1\n",
"end"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2\t4\t6\t8\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2\t4\t8\t6\n",
"2\t6\t4\t8\n",
"2\t6\t8\t4\n",
"2\t8\t4\t6\n",
"2\t8\t6\t4\n",
"4\t2\t6\t8\n",
"4\t2\t8\t6\n",
"4\t6\t2\t8\n",
"4\t6\t8\t2\n",
"4\t8\t2\t6\n",
"4\t8\t6\t2\n",
"6\t2\t4\t8\n",
"6\t2\t8\t4\n",
"6\t4\t2\t8\n",
"6\t4\t8\t2\n",
"6\t8\t2\t4\n",
"6\t8\t4\t2\n",
"8\t2\t4\t6\n",
"8\t2\t6\t4\n",
"8\t4\t2\t6\n",
"8\t4\t6\t2\n",
"8\t6\t2\t4\n",
"8\t6\t4\t2\n"
]
}
],
"prompt_number": 70
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### To solve 24, we check all combinations of +,-,*,/ for every permutation.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's define some our own functions to break our problem into smaller pieces that are easier to figure out. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Here is a function that makes an simple equation easier for people to read:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"function showme(n1,f,n2)\n",
" infix = \"$n1$f$n2 = $(f(n1,n2))\"\n",
" normal_div = replace(infix,\"//\",\"/\")\n",
" cleaned = replace(normal_div,\"= 24/1\",\"= 24\")\n",
" return lpad(cleaned, 18) \n",
"end\n",
"\n",
"showme(24,+,1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 142,
"text": [
"\" 24+1 = 25\""
]
}
],
"prompt_number": 142
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"####Here is a function to test whether one combination of numbers and functions equals 24:\n",
"If the answer is 24, then it prints out the equations in a form that people can read more easily."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"function test_24(a,b,c,d, f1,f2,f3)\n",
" #test whether 4 numbers [a,b,c,d] equal 24, using functions f1, f2 & f3 \n",
" ab = f1(a,b)\n",
" abc = f2(ab,c)\n",
" abcd = f3(abc,d)\n",
" \n",
" if abcd == 24 # do we have a solution?\n",
" return ( showme(a,f1,b) * showme(ab,f2, c) * showme(abc,f3,d) )\n",
" else\n",
" return \"no\"\n",
" end\n",
"end\n",
"\n",
"test_24(12,2,3,4,+,-,*)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 143,
"text": [
"\"no\""
]
}
],
"prompt_number": 143
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Now we can test every possibility: "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"string(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 144,
"text": [
"\"3\""
]
}
],
"prompt_number": 144
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"function solve_24(numbers)\n",
" println( \"\\nSolutions:\\n\")\n",
" count = 0 # let's keep track of how many solutions we find.\n",
" functions = [+,-,*,//]\n",
" \n",
" # loop over the permutations\n",
" for i in 1:24\n",
" a,b,c,d = nthperm(numbers,i)\n",
" #println(\"Checking permutation [$a,$b,$c,$d]\")\n",
" \n",
" # loop over all combinations of the arithmetic functions\n",
" for f1 in functions, f2 in functions, f3 in functions\n",
" answer = t\n",
" est_24(a,b,c,d,f1,f2,f3)\n",
" if answer !=\"no\" \n",
" count = count + 1 \n",
" println(\"$(rpad(string(count)*\":\",3)) $answer\")\n",
" end\n",
" end\n",
" end\n",
" \n",
" if count==1\n",
" println(\"\\nThere is 1 solution.\")\n",
" else\n",
" println(\"\\nThere are $count solutions.\")\n",
" end\n",
"end"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 145,
"text": [
"solve_24 (generic function with 2 methods)"
]
}
],
"prompt_number": 145
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Enter your numbers here, then press [Shift] and [Enter] together"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Enter your numbers here:\n",
"numbers=[1,0,3,0]\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"solve_24(numbers)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Solutions:\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
},
{
"ename": "LoadError",
"evalue": "invalid rational: 0//0\nat In[187]:9",
"output_type": "pyerr",
"traceback": [
"invalid rational: 0//0\nat In[187]:9",
" in Rational at rational.jl:7",
" in // at rational.jl:17",
" in test_24 at In[143]:5"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n"
]
}
],
"prompt_number": 187
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Would you like to try other numbers?\n",
"What numbers do you think have the most solutions? How many?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Thanks!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment