Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q32\n", "Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital." ] }, { "cell_type": "code", "execution_count": 1, "id": "019a77e5-ff2f-4124-a105-b452fcb4a385", "metadata": {}, "outputs": [], "source": [ "def pandigital_products(num_index=0,selected_digits=None,remaining_digits=None):\n", " if remaining_digits is None:\n", " remaining_digits = [1,2,3,4,5,6,7,8,9]\n", " selected_digits = []\n", " result = set()\n", " if num_index == 2 and selected_digits[0]*selected_digits[1] > 10:\n", " return set()\n", " if num_index == 5:\n", " prod_1 = (selected_digits[0]*100+selected_digits[2]*10+selected_digits[3]) * (selected_digits[1]*10+selected_digits[4])\n", " prod_2 = (selected_digits[0]*1000+selected_digits[2]*100+selected_digits[3]*10+selected_digits[4]) * (selected_digits[1])\n", " if sorted(str(prod_1)) == sorted([str(i) for i in remaining_digits]):\n", " return {prod_1}\n", " elif sorted(str(prod_2)) == sorted([str(i) for i in remaining_digits]):\n", " return {prod_2}\n", " else:\n", " return set()\n", " else:\n", " for i,v in enumerate(remaining_digits):\n", " temp = remaining_digits.copy()\n", " result.update(pandigital_products(num_index+1,selected_digits+[temp.pop(i)],temp))\n", " return result\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "48b04317-734e-4c7f-af25-e66513026c05", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 13.3 ms, sys: 397 µs, total: 13.7 ms\n", "Wall time: 12.8 ms\n" ] }, { "data": { "text/plain": [ "45228" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum(pandigital_products())" ] }, { "cell_type": "code", "execution_count": 3, "id": "97e43e46-3bbf-464d-bdd6-182aed1710bb", "metadata": {}, "outputs": [], "source": [ "def factorize(n):\n", " result = []\n", " for i in range(1,int((n)**(0.5))+1):\n", " if (n%i==0):\n", " result.append(i)\n", " #result.append(int(n/i))\n", " result.sort()\n", " return result\n", "\n", "def get_multipy_num(n,factor):\n", " quotient = int(n/factor)\n", " num = []\n", " expressionstr = str(factor)+str(quotient)+str(n)\n", " for digit in expressionstr:\n", " num.append(int(digit))\n", " return sorted(num)\n", "\n", "def judge_pandigitial(num):\n", " if num == list(range(1,10)):\n", " return True\n", " else:\n", " return False\n", " \n", "def pandigital_products_2():\n", " res = []\n", " for i in range(999,10000):\n", " factors_list =factorize(i)\n", " for factor in factors_list:\n", " num = get_multipy_num(i,factor)\n", " if judge_pandigitial(num):\n", " res.append(i)\n", " break\n", " return set(res)" ] }, { "cell_type": "code", "execution_count": 4, "id": "28985d34-5489-41ce-aed8-4f177d692c21", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 13.8 ms, sys: 555 µs, total: 14.4 ms\n", "Wall time: 13.8 ms\n" ] }, { "data": { "text/plain": [ "45228" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time \n", "sum(pandigital_products())" ] }, { "cell_type": "code", "execution_count": null, "id": "59e8ca52-58a2-4a3f-a66e-83022712d0c5", "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 Q31
Next Notebook:
Project Euler Q33
Loading