Last active
October 4, 2018 00:50
-
-
Save razimantv/47ac53a89beb4d9739d9e3cd642e66df to your computer and use it in GitHub Desktop.
Programmatic solution to the "Impossible puzzle"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from collections import defaultdict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"96 1179\n" | |
] | |
} | |
], | |
"source": [ | |
"# For every product and sum, list all the pairs corresponding to it\n", | |
"productset=defaultdict(list)\n", | |
"sumset=defaultdict(list)\n", | |
"for y in range(2,100):\n", | |
" for x in range(2,min(y,101-y)):\n", | |
" productset[x*y].append((x,y))\n", | |
" sumset[x+y].append((x,y))\n", | |
"print (len(sumset), len(productset))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"574\n" | |
] | |
} | |
], | |
"source": [ | |
"# P should be unable to find x and y\n", | |
"# This means we can immediately discard all products with a single way of forming\n", | |
"productset = dict((p,productset[p]) for p in productset if len(productset[p]) > 1)\n", | |
"print (len(productset))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n" | |
] | |
} | |
], | |
"source": [ | |
"# S' knowledge of inability of P to find x and y means that only those sums need to be retained\n", | |
"# of which every possible (x,y) pair results in a still valid product\n", | |
"sumset = dict((s,sumset[s]) for s in sumset if sum(x[0]*x[1] not in productset for x in sumset[s]) == 0)\n", | |
"print (len(sumset))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"86\n" | |
] | |
} | |
], | |
"source": [ | |
"# This information allows P to find x and y\n", | |
"# Means that we need to only consider those products with exactly one (x,y) pair giving a still valid sum\n", | |
"productset=dict((p,[xy for xy in productset[p] if xy[0]+xy[1] in sumset]) for p in productset if sum(xy[0]+xy[1] in sumset for xy in productset[p]) == 1)\n", | |
"print (len(productset))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n" | |
] | |
} | |
], | |
"source": [ | |
"# And further, this information allows S to find x and y\n", | |
"# Means that only those sums with exactly one (x,y) pair with a valid product need to be retained\n", | |
"sumset=dict((s,[xy for xy in sumset[s] if xy[0]*xy[1] in productset]) for s in sumset if sum(xy[0]*xy[1] in productset for xy in sumset[s]) == 1)\n", | |
"print (len(sumset))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{17: [(4, 13)]}\n" | |
] | |
} | |
], | |
"source": [ | |
"# We have only one element remaining in the set of sums\n", | |
"print (sumset)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.6.6+" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment