add problem 43
This commit is contained in:
parent
2ea7b7ceaa
commit
7e9cf29a6b
|
@ -0,0 +1,127 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "aac67a5d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# [Sub-string Divisibility](https://projecteuler.net/problem=43)\n",
|
||||
"\n",
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "dc866d6b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{1406357289, 1430952867, 1460357289, 4106357289, 4130952867, 4160357289}"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from itertools import permutations\n",
|
||||
"\n",
|
||||
"numbers = set()\n",
|
||||
"for digits in permutations((0,1,2,3,4,5,6,7,8,9)):\n",
|
||||
" if is_substring_divisible(digits):\n",
|
||||
" n = sum(10^k * digit for (k, digit) in enumerate(reversed(digits)))\n",
|
||||
" numbers.add(n)\n",
|
||||
" \n",
|
||||
"numbers"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fca588e4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Turns out there's only six of these numbers."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "b6275692",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"16695334890"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sum(numbers)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bfaa46b7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Relevant sequences\n",
|
||||
"* Pandigital numbers: [A050278](https://oeis.org/A050278)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "SageMath 9.5",
|
||||
"language": "sage",
|
||||
"name": "sagemath"
|
||||
},
|
||||
"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.11.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Loading…
Reference in New Issue