From 4078fde9f9337af8c08c8a28c4d72b575a689ff1 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 17 Mar 2017 09:00:35 +0000 Subject: [PATCH] Done puzzle 44 --- euler44.ipynb | 93 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/euler44.ipynb b/euler44.ipynb index df13e12..2762a84 100644 --- a/euler44.ipynb +++ b/euler44.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -13,7 +13,7 @@ ":pentagonal" ] }, - "execution_count": 1, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -24,6 +24,95 @@ "end" ] }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2167, 1020 -> 5482660\n", + "2167, 1020 -> 5482660\n" + ] + } + ], + "source": [ + "pentagonals = Set.new()\n", + "\n", + "found = false\n", + "sum_index = 1\n", + "best_k = 0\n", + "best_j = 0\n", + "best_diff = pentagonal(5000)\n", + "\n", + "while !found do\n", + " pentagonals << pentagonal(sum_index)\n", + " ((sum_index/2)...sum_index).each do |j|\n", + " k = sum_index - j\n", + " diff = pentagonal(j) - pentagonal(k)\n", + " if pentagonals.include?(diff) && pentagonals.include?(pentagonal(j)+pentagonal(k))\n", + " found = true\n", + " puts \"#{j}, #{k} -> #{diff}\"\n", + " if diff < best_diff\n", + " best_diff = diff\n", + " best_k = k\n", + " best_j = j\n", + " end\n", + " end\n", + " end\n", + " sum_index += 1\n", + "end\n", + "puts \"#{best_j}, #{best_k} -> #{best_diff}\"" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "true" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pentagonals.include?(pentagonal(2167))" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "210" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pentagonal(12)" + ] + }, { "cell_type": "code", "execution_count": null, -- 2.34.1