From 3698e639b98d9f278f500f84998755b04dea1418 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Tue, 6 Feb 2018 16:51:03 +0100 Subject: [PATCH] Tested simple sum of factor function. --- ipython/EulerProblem021.ipynb | 100 ++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 11 deletions(-) diff --git a/ipython/EulerProblem021.ipynb b/ipython/EulerProblem021.ipynb index f81cc68..5741416 100644 --- a/ipython/EulerProblem021.ipynb +++ b/ipython/EulerProblem021.ipynb @@ -30,8 +30,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "5.759276217543773\n", - "0.3214989060045452\n" + "5.062845234464119\n", + "0.3188800166435408\n" ] } ], @@ -80,8 +80,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "6.244833890762658\n", - "5.688215703110259\n" + "6.370271180404346\n", + "5.785088868397899\n" ] } ], @@ -135,7 +135,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def group(xs):\n", @@ -175,7 +177,9 @@ { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def prime_group_to_factors(group):\n", @@ -196,7 +200,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def combine_factors(xs, ys):\n", @@ -215,7 +221,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def combine_factors(xss):\n", @@ -228,7 +236,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def get_divisors(n):\n", @@ -254,7 +264,9 @@ { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def sum_of_proper_divisors(number):\n", @@ -302,7 +314,73 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "There is probably a much faster algorithm which works by caching the factors for previous numbers. Let's say we got the factors for $f(4) = (1, 2, 4)$. And our number new number is $28$. Ah, that does not really work as I thought. I am sure there is a smarter way." + "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": {}, + "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": {}, + "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." ] } ],