{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Euler Problem 32\n",
    "\n",
    "We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.\n",
    "\n",
    "The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.\n",
    "\n",
    "Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.\n",
    "\n",
    "HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We start by thinking about how many digits are used for multiplicand/multiplier/product. Obviously, the product must have more then three digits. Can it have more then 4 digits? Obviously it can because then there are only four digits for multiplicand and multiplier which is always smaller than ten thousand. So our product must be in the form $12 \\cdot 345 = 5789$. This makes the rest very easy in Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45228"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from itertools import permutations\n",
    "\n",
    "def is_solution(s):\n",
    "    a, b, c = int(s[0:2]), int(s[2:5]), int(s[5:])\n",
    "    if a * b == c:\n",
    "        return c\n",
    "    a, b, c = int(s[0:1]), int(s[1:5]), int(s[5:])  \n",
    "    if a * b == c:\n",
    "        return c\n",
    "    return 0\n",
    "\n",
    "assert(is_solution(\"391867254\") == 7254)\n",
    "assert(is_solution(\"391867245\") == 0)\n",
    "\n",
    "\n",
    "s = sum(set([is_solution(\"\".join(p)) for p in permutations(\"123456789\")]))\n",
    "assert(s == 45228)\n",
    "s"
   ]
  }
 ],
 "metadata": {
  "completion_date": "Wed, 30 Aug 2017, 14:25",
  "kernelspec": {
   "display_name": "Python 3",
   "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.5.4"
  },
  "tags": [
   "pandigital",
   "products"
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
