From 110ba1cfaece7fa9074ad68eb31e29b8afd31f3d Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 17 Mar 2017 09:20:27 +0000 Subject: [PATCH] Done puzzle 45 --- euler45.ipynb | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 euler45.ipynb diff --git a/euler45.ipynb b/euler45.ipynb new file mode 100644 index 0000000..70a5dc9 --- /dev/null +++ b/euler45.ipynb @@ -0,0 +1,118 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that the _n_ th hexagonal number is the $2(n-1)+1$th triangle number. Therefore, we can find a number that is both pentagonal and hexagonal.\n", + "\n", + "The _n_ hexagonal number is never smaller than the _n_ th pentagonal number, so the idea is to generate the next hexagonal number, then all pentagonal numbers until we find one that is larger than the largest hexagonal one found so far." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":pentagonal" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def pentagonal(n)\n", + " n * (3 * n - 1) / 2\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":hexagonal" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def hexagonal(n)\n", + " n * (2 * n - 1)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "31977, 27693, 1533776805\n" + ] + } + ], + "source": [ + "h_i = 144\n", + "p_i = 1\n", + "\n", + "finished = false\n", + "\n", + "while !finished\n", + " while pentagonal(p_i) < hexagonal(h_i)\n", + " p_i += 1\n", + " end\n", + " if pentagonal(p_i) == hexagonal(h_i)\n", + " puts \"#{p_i}, #{h_i}, #{pentagonal(p_i)}\"\n", + " finished = true\n", + " end\n", + " h_i += 1\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Ruby 2.4.0", + "language": "ruby", + "name": "ruby" + }, + "language_info": { + "file_extension": ".rb", + "mimetype": "application/x-ruby", + "name": "ruby", + "version": "2.4.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} -- 2.34.1