Done puzzle 64
[project-euler.git] / euler45.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "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",
8 "\n",
9 "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."
10 ]
11 },
12 {
13 "cell_type": "code",
14 "execution_count": 8,
15 "metadata": {
16 "collapsed": false
17 },
18 "outputs": [
19 {
20 "data": {
21 "text/plain": [
22 ":pentagonal"
23 ]
24 },
25 "execution_count": 8,
26 "metadata": {},
27 "output_type": "execute_result"
28 }
29 ],
30 "source": [
31 "def pentagonal(n)\n",
32 " n * (3 * n - 1) / 2\n",
33 "end"
34 ]
35 },
36 {
37 "cell_type": "code",
38 "execution_count": 9,
39 "metadata": {
40 "collapsed": false
41 },
42 "outputs": [
43 {
44 "data": {
45 "text/plain": [
46 ":hexagonal"
47 ]
48 },
49 "execution_count": 9,
50 "metadata": {},
51 "output_type": "execute_result"
52 }
53 ],
54 "source": [
55 "def hexagonal(n)\n",
56 " n * (2 * n - 1)\n",
57 "end"
58 ]
59 },
60 {
61 "cell_type": "code",
62 "execution_count": 11,
63 "metadata": {
64 "collapsed": false
65 },
66 "outputs": [
67 {
68 "name": "stdout",
69 "output_type": "stream",
70 "text": [
71 "31977, 27693, 1533776805\n"
72 ]
73 }
74 ],
75 "source": [
76 "h_i = 144\n",
77 "p_i = 1\n",
78 "\n",
79 "finished = false\n",
80 "\n",
81 "while !finished\n",
82 " while pentagonal(p_i) < hexagonal(h_i)\n",
83 " p_i += 1\n",
84 " end\n",
85 " if pentagonal(p_i) == hexagonal(h_i)\n",
86 " puts \"#{p_i}, #{h_i}, #{pentagonal(p_i)}\"\n",
87 " finished = true\n",
88 " end\n",
89 " h_i += 1\n",
90 "end"
91 ]
92 },
93 {
94 "cell_type": "code",
95 "execution_count": null,
96 "metadata": {
97 "collapsed": true
98 },
99 "outputs": [],
100 "source": []
101 }
102 ],
103 "metadata": {
104 "kernelspec": {
105 "display_name": "Ruby 2.4.0",
106 "language": "ruby",
107 "name": "ruby"
108 },
109 "language_info": {
110 "file_extension": ".rb",
111 "mimetype": "application/x-ruby",
112 "name": "ruby",
113 "version": "2.4.0"
114 }
115 },
116 "nbformat": 4,
117 "nbformat_minor": 0
118 }