{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Euler Problem 21\n",
    "\n",
    "Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).\n",
    "If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.\n",
    "\n",
    "For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.\n",
    "\n",
    "Evaluate the sum of all the amicable numbers under 10000."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We reuse the sieve of eratosthenes (which we called get_primes_smaller previously). What annoys me about the implementation is that we do not stop scanning for prospects once the current prime squared is greater than the limit. Let's test if it returns the same result and compare the execution time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.062845234464119\n",
      "0.3188800166435408\n"
     ]
    }
   ],
   "source": [
    "def get_primes_smaller(limit):\n",
    "    primes = []\n",
    "    prospects = [n for n in range(2, limit)]\n",
    "    while prospects:\n",
    "        p = prospects[0]\n",
    "        prospects = [x for x in prospects if x % p != 0]\n",
    "        primes.append(p)\n",
    "    return primes\n",
    "\n",
    "def sieve_of_eratosthenes(limit):\n",
    "    primes = []\n",
    "    prospects = [n for n in range(2, limit)]\n",
    "    while prospects:\n",
    "        p = prospects[0]\n",
    "        prospects = [x for x in prospects if x % p != 0]\n",
    "        primes.append(p)\n",
    "        if p * p > limit:\n",
    "            break\n",
    "    primes += prospects\n",
    "    return primes    \n",
    "\n",
    "import timeit\n",
    "import functools\n",
    "print(timeit.timeit(functools.partial(get_primes_smaller, 10000), number=100))\n",
    "print(timeit.timeit(functools.partial(sieve_of_eratosthenes, 10000), number=100))\n",
    "assert(get_primes_smaller(10000) == sieve_of_eratosthenes(10000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Okay, this difference is sick actually. Interesting to see what the difference between O(sqrt(n)) and O(n) can mean. Now we can use the primes to do the factorization. I will again apply some optimizations and see how it turns out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.370271180404346\n",
      "5.785088868397899\n"
     ]
    }
   ],
   "source": [
    "def get_prime_factors_old(number):\n",
    "    prime_generator = sieve_of_eratosthenes(number // 2)\n",
    "    remainder = number\n",
    "    factors = []\n",
    "    for p in prime_generator:\n",
    "        while remainder % p == 0:\n",
    "            remainder //= p\n",
    "            factors.append(p)\n",
    "        if remainder == 1 or p * p > number:\n",
    "            break\n",
    "    if remainder != 1:\n",
    "        factors.append(remainder)\n",
    "    return factors\n",
    "\n",
    "def get_prime_factors(number):\n",
    "    remainder = number\n",
    "    factors = []\n",
    "    for p in sieve_of_eratosthenes(number // 2):\n",
    "        while remainder % p == 0:\n",
    "            remainder //= p\n",
    "            factors.append(p)\n",
    "        if remainder == 1:\n",
    "            break\n",
    "        if p > remainder or p * p > number:\n",
    "            factors.append(remainder)\n",
    "            break\n",
    "    return factors\n",
    "\n",
    "import timeit\n",
    "import functools\n",
    "print(timeit.timeit(functools.partial(get_prime_factors_old, 1000000), number=10))\n",
    "print(timeit.timeit(functools.partial(get_prime_factors, 1000000), number=10))\n",
    "assert(get_prime_factors_old(100000) == get_prime_factors(100000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Okay, actually no big difference. Kind of expected if you check how similar the implementations are. I though stopping if p is greater than the remainder may be a common case.\n",
    "\n",
    "Now we can use the prime factors to get all factors. For this each prime number can be used 0 to n times where n is the number how often the prime occurs in the prime factorization. For example, all possible fators for the primes $2, 2, 3$ are $2^0 \\times 3^0, 2^1 \\times 3^0, 2^2 \\times 3^0, 2^0 \\times 3^1, 2^1 \\times 3^1, 2^2 \\times 3^1$ which results in $1, 2, 4, 3, 6, 12$.\n",
    "\n",
    "So we proceed in the following way. Once we have the prime factors we group then. Then we calculate the factors for each group. For example for $2,2$ the factors would be $1, 2, 4$ as explained in the previous paragraph. Then we calculate the combinations of all factor groups."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def group(xs):\n",
    "    from functools import reduce\n",
    "    rs = []\n",
    "    def f(xss, x):\n",
    "        if xss and x in xss[-1]:\n",
    "            xss[-1].append(x)\n",
    "        else:\n",
    "            xss.append([x])\n",
    "        return xss\n",
    "    return reduce(f, xs, [])\n",
    "\n",
    "assert(group([1, 2, 2, 2, 4, 4, 5]) == [[1], [2, 2, 2], [4, 4], [5]])\n",
    "\n",
    "def group(xs):\n",
    "    r = []\n",
    "    for x in xs:\n",
    "        if r and r[-1][0] == x:\n",
    "            r[-1].append(x)\n",
    "        else:\n",
    "            r.append([x])\n",
    "    return r\n",
    "\n",
    "assert(group([1, 2, 2, 2, 4, 4, 5]) == [[1], [2, 2, 2], [4, 4], [5]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see I had a fancy version with reduce first, but the loop is way more readable. We iterate over the input list. If the current item is in the last sublist of the return list we add it to the list. Otherwise we append a new sublist with the item.\n",
    "\n",
    "Next we write a function that calculates the resulting factors for a factor group."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def prime_group_to_factors(group):\n",
    "    f = group[0]\n",
    "    n = len(group)\n",
    "    return [f**i for i in range(n + 1)]\n",
    "\n",
    "assert(prime_group_to_factors([2, 2, 2]) == [1, 2, 4, 8])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "No we only have to get all factors by calculating the combination of two factor lists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def combine_factors(xs, ys):\n",
    "    return [x * y for y in ys for x in xs]\n",
    "\n",
    "assert(combine_factors([1, 2, 4], [1, 3]) == [1, 2, 4, 3, 6, 12])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now actually we want to combine an arbitrary number of factor lists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def combine_factors(xss):\n",
    "    ys = [1]\n",
    "    for xs in xss:\n",
    "        ys = [x * y for y in ys for x in xs]\n",
    "    return ys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_divisors(n):\n",
    "    prime_factors = get_prime_factors(n)\n",
    "    prime_groups = group(prime_factors)\n",
    "    factor_groups = list(map(prime_group_to_factors, prime_groups))\n",
    "    divisors = combine_factors(factor_groups)\n",
    "    divisors.sort()\n",
    "    return divisors\n",
    "    \n",
    "assert(get_divisors(220) == [1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, 220])\n",
    "assert(get_divisors(13) == [1, 13])\n",
    "assert(get_divisors(26) == [1, 2, 13, 26])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can finally start to get the solution. Remember that we only want to get the sum of proper divisors which are the divisors withouth the number itself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sum_of_proper_divisors(number):\n",
    "    return sum(get_divisors(number)[:-1])\n",
    "\n",
    "assert(sum_of_proper_divisors(220) == 284)\n",
    "assert(sum_of_proper_divisors(284) == 220)\n",
    "\n",
    "def is_amicable(number):\n",
    "    s = sum_of_proper_divisors(number)\n",
    "    if s == number:\n",
    "        return False\n",
    "    return number == sum_of_proper_divisors(s)\n",
    "\n",
    "assert(is_amicable(220))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The if statement is very import because a number pair $a, b$ is only amicable if $a \\neq b$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31626\n"
     ]
    }
   ],
   "source": [
    "amicable_till_10000 = [i for i in range(1, 10000) if is_amicable(i)]\n",
    "assert(sum(amicable_till_10000) == 31626)\n",
    "print(sum(amicable_till_10000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I found some brute force solutions in the Euler forum and they claim to have a sick performance. I want to compare them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "108.68903765424807\n"
     ]
    }
   ],
   "source": [
    "def brute_force():\n",
    "    return sum([i for i in range(1, 10000) if is_amicable(i)])\n",
    "\n",
    "import timeit\n",
    "print(timeit.timeit(brute_force, number=10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7862764837699387\n"
     ]
    }
   ],
   "source": [
    "def sum_of_proper_divisors(n):\n",
    "    from math import sqrt\n",
    "    s = 1\n",
    "    for i in range(2, int(sqrt(n)) + 1):\n",
    "        if n % i == 0:\n",
    "            s += i\n",
    "            s += (n // i)\n",
    "    return s\n",
    "\n",
    "assert(sum_of_proper_divisors(220) == 284)\n",
    "       \n",
    "def is_amicable(number):\n",
    "    s = sum_of_proper_divisors(number)\n",
    "    if s == number:\n",
    "        return False\n",
    "    return number == sum_of_proper_divisors(s)\n",
    "\n",
    "def brute_force():\n",
    "    return sum([i for i in range(1, 10000) if is_amicable(i)])   \n",
    "    \n",
    "import timeit\n",
    "print(timeit.timeit(brute_force, number=10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is actually really embarrassing. Especially I was aware of the sqrt trick all the way but did not manage to put it into use properly. Another example where greate engineer will leave you far behind compared to great thinking.\n",
    "\n",
    "**Supplement:** Also I have discovered that my sum of proper divisors function has a bug when n is a square, because in that case _i == (n // 1)_ which adds this value two times. So there is no amicable number under 10000 which is also a square because otherwise we would have failed."
   ]
  }
 ],
 "metadata": {
  "completion_date": "Fri, 5 Sep 2014, 14:39",
  "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": [
   "amicable",
   "sieve of eratosthenes",
   "factors",
   "timeit"
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
