"If you *really* wanted to, you could work this out with pen and paper using [divisibility rules](https://en.wikipedia.org/wiki/Divisibility_rule). Alternatively, it's easy to brute force. First we write a function to test for the given property."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "71136ed1",
"metadata": {},
"outputs": [],
"source": [
"def is_substring_divisible(digits):\n",
" for idx, divisor in enumerate((2, 3, 5, 7, 11, 13, 17), start=1):\n",
" x, y, z = digits[idx:idx+3]\n",
" n = 100*x + 10*y + z\n",
" if n % divisor != 0:\n",
" return False\n",
" \n",
" return True"
]
},
{
"cell_type": "markdown",
"id": "3560bcb4",
"metadata": {},
"source": [
"Then we test every permutation of 0 through 9. There's $10! = 3628800$ such permutations, which may seem like a lot, and you could apply some logic to reduce the search space some (e.g. $d_4$ must be 0, 2, 4, 6, or 8), but the test runs relatively quickly for each permutation as-is."
"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)."