{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cyclical figurate numbers (Euler Problem 61)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "[https://projecteuler.net/problem=61](https://projecteuler.net/problem=61)\n",
    "\n",
    "Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:\n",
    "\n",
    "Triangle\t \tP3,n=n(n+1)/2\t \t1, 3, 6, 10, 15, ...\n",
    "\n",
    "Square\t \tP4,n=n2\t \t1, 4, 9, 16, 25, ...\n",
    "\n",
    "Pentagonal\t \tP5,n=n(3n−1)/2\t \t1, 5, 12, 22, 35, ...\n",
    "\n",
    "Hexagonal\t \tP6,n=n(2n−1)\t \t1, 6, 15, 28, 45, ...\n",
    "\n",
    "Heptagonal\t \tP7,n=n(5n−3)/2\t \t1, 7, 18, 34, 55, ...\n",
    "\n",
    "Octagonal\t \tP8,n=n(3n−2)\t \t1, 8, 21, 40, 65, ...\n",
    "\n",
    "The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.\n",
    "\n",
    "The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).\n",
    "Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882), is represented by a different number in the set.\n",
    "This is the only set of 4-digit numbers with this property.\n",
    "Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_four_digit_numbers(function):\n",
    "    r = []\n",
    "    n = 1\n",
    "    f = function\n",
    "    while f(n) < 10000:\n",
    "        if f(n) > 999:\n",
    "            r.append(f(n))\n",
    "        n += 1\n",
    "    return r\n",
    "\n",
    "triangles = get_four_digit_numbers(lambda n: n * (n + 1) // 2)\n",
    "squares = get_four_digit_numbers(lambda n: n**2)\n",
    "pentas = get_four_digit_numbers(lambda n: n * (3 * n - 1) // 2)\n",
    "hexas = get_four_digit_numbers(lambda n: n * (2 * n - 1))\n",
    "heptas = get_four_digit_numbers(lambda n: n * (5*n - 3) // 2)\n",
    "octas = get_four_digit_numbers(lambda n: n * (3*n - 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def is_cyclic(a, b):\n",
    "    return str(a)[-2:] == str(b)[:2]\n",
    "\n",
    "assert(is_cyclic(3328, 2877))\n",
    "assert(is_cyclic(3329, 2877) == False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8256, 5625, 2512, 1281, 8128, 2882]\n",
      "28684\n"
     ]
    }
   ],
   "source": [
    "def search_solution(aggregator, polygonals):\n",
    "    if not polygonals:\n",
    "        if is_cyclic(aggregator[-1], aggregator[0]):\n",
    "            return aggregator\n",
    "        else:\n",
    "            return []\n",
    "\n",
    "    if not aggregator:\n",
    "        for polygonal in polygonals:\n",
    "            for number in polygonal:\n",
    "                aggregator.append(number)\n",
    "                s = search_solution(aggregator, [p for p in polygonals if p != polygonal])\n",
    "                if s:\n",
    "                    return s\n",
    "                aggregator.pop()\n",
    "\n",
    "    for polygonal in polygonals:\n",
    "        for number in polygonal:\n",
    "            if is_cyclic(aggregator[-1], number) and not number in aggregator:\n",
    "                aggregator.append(number)\n",
    "                s = search_solution(aggregator, [p for p in polygonals if p != polygonal])\n",
    "                if s:\n",
    "                    return s\n",
    "                aggregator.pop()\n",
    "    return []\n",
    "\n",
    "s = search_solution([], [triangles, squares, pentas, hexas, heptas, octas])\n",
    "print(s)\n",
    "s = sum(s)\n",
    "print(s)\n",
    "assert(s == 28684)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "completion_date": "Sun, 6 Jan 2019, 05:32",
  "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": [
   "search",
   "cyclic"
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
