Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q43\n", "Find the sum of all 0 to 9 pandigital numbers with sub-string divisibility property." ] }, { "cell_type": "code", "execution_count": 1, "id": "820c6f59-d5d1-4f67-a379-b41d8bb337ac", "metadata": {}, "outputs": [], "source": [ "def is_sub_string_divisible(n):\n", " str_n = str(n)\n", " len_n = len(n)\n", " if len_n >= 4 and int(str_n[1:4]) % 2: return False\n", " if len_n >= 5 and int(str_n[2:5]) % 3: return False\n", " if len_n >= 6 and int(str_n[3:6]) % 5: return False\n", " if len_n >= 7 and int(str_n[4:7]) % 7: return False\n", " if len_n >= 8 and int(str_n[5:8]) % 11: return False\n", " if len_n >= 9 and int(str_n[6:9]) % 13: return False\n", " if len_n >= 10 and int(str_n[7:10]) % 17: return False\n", " return True\n", "\n", "def distinct_digit_generator(candidates,selected=None):\n", " if selected is None:\n", " selected = []\n", " if len(candidates) == 0:\n", " yield int(''.join(str(i) for i in selected))\n", " for i in range(len(candidates)):\n", " temp_c = candidates.copy()\n", " temp_s = selected+[temp_c.pop(i)]\n", " if is_sub_string_divisible(''.join(str(i) for i in temp_s)):\n", " yield from distinct_digit_generator(temp_c,temp_s)\n", " \n", "def sub_string_divisible_num():\n", " return {i for i in distinct_digit_generator([0,1,2,3,4,5,6,7,8,9])}" ] }, { "cell_type": "code", "execution_count": 2, "id": "1ef770af-4b63-4d07-abef-0cbe9483e888", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 143 ms, sys: 0 ns, total: 143 ms\n", "Wall time: 140 ms\n" ] }, { "data": { "text/plain": [ "16695334890" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum(sub_string_divisible_num())" ] }, { "cell_type": "code", "execution_count": null, "id": "392b184b-9336-4add-b21e-a46ebd6740de", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q42
Next Notebook:
Project Euler Q44
Loading