From b5b0504d6a1ee633d577d72c502c97500118dc47 Mon Sep 17 00:00:00 2001 From: filifa Date: Mon, 21 Apr 2025 00:23:51 -0400 Subject: [PATCH] add problem 32 --- notebooks/problem0032.ipynb | 74 +++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 notebooks/problem0032.ipynb diff --git a/notebooks/problem0032.ipynb b/notebooks/problem0032.ipynb new file mode 100644 index 0000000..23e3636 --- /dev/null +++ b/notebooks/problem0032.ipynb @@ -0,0 +1,74 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "004a4666", + "metadata": {}, + "source": [ + "# [Pandigital Products](https://projecteuler.net/problem=32)\n", + "\n", + "The product of any number with a four digit number will be at least four digits, meaning it is possible the multiplicand, multiplier, and product are pandigital, since there would be at least nine total digits. In contrast, the product of any number and a five digit number will be at least five digits, so the three-number combo will not be pandigital - it will have at least 11 digits.\n", + "\n", + "This allows to set bounds on our brute force. We will make sure neither factor is more than four digits." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5d61d14d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "45228" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def is_pandigital(n):\n", + " return ''.join(sorted(n)) == \"123456789\"\n", + "\n", + "\n", + "products = set()\n", + "for p in range(1, 10000):\n", + " for q in range(p, 10000):\n", + " product = p * q\n", + " \n", + " s = f\"{p}{q}{product}\"\n", + " if len(s) > 9:\n", + " break\n", + " \n", + " if is_pandigital(s):\n", + " products.add(product)\n", + "\n", + "sum(products)" + ] + } + ], + "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 +}