Skip to content

Instantly share code, notes, and snippets.

@cjtu
Last active January 22, 2019 00:15
Show Gist options
  • Save cjtu/cdfb0f7f330a06c035288c2e7cacc4af to your computer and use it in GitHub Desktop.
Save cjtu/cdfb0f7f330a06c035288c2e7cacc4af to your computer and use it in GitHub Desktop.
Itsa me, Numberwang
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Numberwang\n",
"Run the cell below to load up the functions. Run by clicking the cell, then clicking the `>|Run` button above, or `ctrl+enter`. The `In [ ]` will turn into an `In [1]`. If ever the `In [ ]` gets stuck on `In [*]` (which it will, because this is bad code), go up to `Kernerl > Restart > [Restart button]` then re-run the cells starting from the top."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"def is_square(num):\n",
" return int(math.sqrt(num)) == math.sqrt(num)\n",
"\n",
"def find_next(remaining, sequence, verbose, solved):\n",
" for i in range(len(remaining)):\n",
" if is_square(sequence[-1] + remaining[i]):\n",
" if len(remaining) == 1:\n",
" if verbose:\n",
" print(\" SOLUTION: \", sequence + [remaining[i]])\n",
" return True\n",
" else:\n",
" solved = find_next(remaining[:i] + remaining[i+1:], sequence + [remaining[i]], verbose, solved)\n",
" return solved\n",
" \n",
"\n",
"def get_consec_squares(start, end, verbose):\n",
" solved = False\n",
" interval = list(range(start, end+1))\n",
" for i in interval:\n",
" remaining = interval.copy()\n",
" remaining.remove(i)\n",
" sequence = [i]\n",
" if find_next(remaining, sequence, verbose, solved):\n",
" solved = True\n",
" return solved\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make sure you run the code cell above first!\n",
"\n",
"Now pick the intervals to loop over (the wider the interval, the longer it'll take to run). Set `verbose = True` if you want to see the solutions.\n",
"\n",
"Click the block below and then click the `>|Run` button above, or `ctrl+enter`."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Interval: (1,2) fails\n",
"Interval: (1,3) fails\n",
"Interval: (1,4) fails\n",
"Interval: (1,5) fails\n",
"Interval: (1,6) fails\n",
"Interval: (1,7) fails\n",
"Interval: (1,8) fails\n",
"Interval: (1,9) fails\n",
"Interval: (1,10) fails\n",
"Interval: (1,11) fails\n",
"Interval: (1,12) fails\n",
"Interval: (1,13) fails\n",
"Interval: (1,14) fails\n",
"Interval: (1,15) works\n",
"Interval: (1,16) works\n",
"Interval: (1,17) works\n"
]
}
],
"source": [
"start_min = 1 # The start of the interval\n",
"start_max = 1 \n",
"end_min = 2\n",
"end_max = 17\n",
"verbose = False\n",
"\n",
"#=========================================#\n",
"for start in range(start_min, start_max+1):\n",
" for end in range(end_min, end_max+1):\n",
" interval = \"Interval: (\"+str(start) + \",\" + str(end) + \")\"\n",
" if verbose:\n",
" print(interval)\n",
" get_consec_squares(1, end, verbose)\n",
" else:\n",
" if get_consec_squares(1, end, verbose):\n",
" print(interval, \"works\")\n",
" else:\n",
" print(interval, \"fails\")"
]
}
],
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment