Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q38\n", "What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with \\((1,2, … ,n)\\) where \\(n > 1\\)?" ] }, { "cell_type": "code", "execution_count": 1, "id": "255771bb-8696-4c1c-aeb2-f56c3bc62237", "metadata": {}, "outputs": [], "source": [ "def is_pandigital(num_string):\n", " return sorted(num_string) == [str(n) for n in range(1,10)]\n", " \n", "def unique_digit_num(n, selected_digits = None, candidate_digits = None):\n", " if selected_digits is None and candidate_digits is None:\n", " selected_digits = []\n", " candidate_digits = list(range(1,10))\n", " if n == 0:\n", " return [int(''.join(str(i) for i in selected_digits))]\n", " result = []\n", " for i in range(len(candidate_digits)):\n", " temp_candidate_digits = candidate_digits.copy()\n", " result += unique_digit_num(n-1,selected_digits+[temp_candidate_digits.pop(i)],temp_candidate_digits)\n", " return result\n", "\n", "def largest_pandigital_multiples():\n", " for a in unique_digit_num(4,[],[2,3,4,5,6,7,9])[::-1]:\n", " b = a*2\n", " str_ab = str(a)+str(b)\n", " if is_pandigital(str_ab):\n", " return int(str_ab)" ] }, { "cell_type": "code", "execution_count": 2, "id": "3d047ff9-52dc-4b20-b614-4192212ea5ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 673 µs, sys: 181 µs, total: 854 µs\n", "Wall time: 855 µs\n" ] }, { "data": { "text/plain": [ "932718654" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "largest_pandigital_multiples()" ] }, { "cell_type": "code", "execution_count": null, "id": "bbfec14e-434a-4379-ab3b-6a07bd937113", "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 Q37
Next Notebook:
Project Euler Q39
Loading