"The search term for this concept is an [anomalous cancellation](https://en.wikipedia.org/wiki/Anomalous_cancellation). We can write a function that checks if an anomalous cancellation can happen by storing the digits of the numerator and denominator into four variables total, then checking four separate cases for if a digit can be \"cancelled.\""
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d4e94963",
"metadata": {},
"outputs": [],
"source": [
"def digits(n):\n",
" return (n // 10, n % 10)\n",
"\n",
"\n",
"def can_cancel_digits(n, d):\n",
" x, y = digits(n)\n",
" z, w = digits(d)\n",
"\n",
" f = QQ(n/d)\n",
"\n",
" if x == z and w != 0 and y/w == f:\n",
" return True\n",
" elif x == w and y/z == f:\n",
" return True\n",
" elif y == z and w != 0 and x/w == f:\n",
" return True\n",
" elif y == w and w != 0 and x/z == f:\n",
" return True\n",
"\n",
" return False"
]
},
{
"cell_type": "markdown",
"id": "a68309e9",
"metadata": {},
"source": [
"We're only dealing with two-digit numerators and denominators, so this is easy to brute force."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "756614f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{(16, 64), (19, 95), (26, 65), (49, 98)}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fractions = set()\n",
"for n in range(10, 100):\n",
" for d in range(n + 1, 100):\n",
" if can_cancel_digits(n, d):\n",
" fractions.add((n, d))\n",
"\n",
"fractions"
]
},
{
"cell_type": "markdown",
"id": "6d3fb43b",
"metadata": {},
"source": [
"There's only four such fractions."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "90f47a7f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prod(QQ(n/d) for (n, d) in fractions).denominator()"
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."