From 7e9cf29a6bb30e3d147543620d964efe24893817 Mon Sep 17 00:00:00 2001 From: filifa Date: Mon, 26 May 2025 12:20:29 -0400 Subject: [PATCH] add problem 43 --- notebooks/problem0043.ipynb | 127 ++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 notebooks/problem0043.ipynb diff --git a/notebooks/problem0043.ipynb b/notebooks/problem0043.ipynb new file mode 100644 index 0000000..d13b30f --- /dev/null +++ b/notebooks/problem0043.ipynb @@ -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 +}