"cells": [
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": 2,
"metadata": {
"collapsed": false
},
":pentagonal"
]
},
- "execution_count": 1,
+ "execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
"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,