{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cubic permutations (Euler Problem 62)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "[https://projecteuler.net/problem=62](https://projecteuler.net/problem=62)\n",
    "\n",
    "The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.\n",
    "\n",
    "Find the smallest cube for which exactly five permutations of its digits are cube."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cubes = [n * n * n for n in range(1, 5000)]\n",
    "cubes_set = set(cubes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[127035954683, 352045367981, 373559126408, 569310543872, 589323567104]\n",
      "127035954683\n"
     ]
    }
   ],
   "source": [
    "solutions = {}\n",
    "\n",
    "for n in range(1, 10000):\n",
    "    cube = n * n * n\n",
    "    try:\n",
    "        key = \"\".join(sorted(str(cube)))\n",
    "        solutions[key].append(cube)\n",
    "        if len(solutions[key]) > 4:\n",
    "            print(solutions[key])\n",
    "            s = solutions[key][0]\n",
    "            break\n",
    "    except KeyError:\n",
    "        solutions[key] = [cube]\n",
    "\n",
    "print(s)\n",
    "assert(s == 127035954683)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "completion_date": "Sun, 6 Jan 2019, 05:47",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python3.6",
   "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.6.5"
  },
  "tags": [
   "cubes"
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
