Done puzzle 64
[project-euler.git] / euler62.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [
8 {
9 "data": {
10 "text/plain": [
11 "true"
12 ]
13 },
14 "execution_count": 1,
15 "metadata": {},
16 "output_type": "execute_result"
17 }
18 ],
19 "source": [
20 "require_relative 'array-numbers'"
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": 52,
26 "metadata": {},
27 "outputs": [
28 {
29 "data": {
30 "text/plain": [
31 "865"
32 ]
33 },
34 "execution_count": 52,
35 "metadata": {},
36 "output_type": "execute_result"
37 }
38 ],
39 "source": [
40 "cubes = Hash.new {|h, k| h[k] = []}\n",
41 "cube_roots = {}\n",
42 "(100..1000).each do |i|\n",
43 " c = (i ** 3)\n",
44 " p = c.to_digits.sort.join\n",
45 " # cubes[p] = (cubes[p] + [c])\n",
46 " cubes[p] << c\n",
47 " cube_roots[c] = i\n",
48 "end\n",
49 "cubes.length"
50 ]
51 },
52 {
53 "cell_type": "code",
54 "execution_count": 53,
55 "metadata": {},
56 "outputs": [
57 {
58 "data": {
59 "text/plain": [
60 "{\"01234566\"=>[41063625, 56623104, 66430125]}"
61 ]
62 },
63 "execution_count": 53,
64 "metadata": {},
65 "output_type": "execute_result"
66 }
67 ],
68 "source": [
69 "cubes.select {|k, v| v.length >= 3}"
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 42,
75 "metadata": {},
76 "outputs": [
77 {
78 "data": {
79 "text/plain": [
80 "345"
81 ]
82 },
83 "execution_count": 42,
84 "metadata": {},
85 "output_type": "execute_result"
86 }
87 ],
88 "source": [
89 "cube_roots[41063625]"
90 ]
91 },
92 {
93 "cell_type": "code",
94 "execution_count": 58,
95 "metadata": {},
96 "outputs": [
97 {
98 "data": {
99 "text/plain": [
100 "127035954683"
101 ]
102 },
103 "execution_count": 58,
104 "metadata": {},
105 "output_type": "execute_result"
106 }
107 ],
108 "source": [
109 "target = 5\n",
110 "\n",
111 "cubes = Hash.new {|h, k| h[k] = []}\n",
112 "finished = false\n",
113 "i = 100\n",
114 "while ! finished\n",
115 " c = (i ** 3)\n",
116 " p = c.to_digits.sort.join\n",
117 " cubes[p] << c\n",
118 " if cubes[p].length == target\n",
119 " finished = true\n",
120 " else\n",
121 " i += 1\n",
122 " end\n",
123 "end\n",
124 "\n",
125 "cubes[p].min"
126 ]
127 },
128 {
129 "cell_type": "code",
130 "execution_count": null,
131 "metadata": {
132 "collapsed": true
133 },
134 "outputs": [],
135 "source": []
136 }
137 ],
138 "metadata": {
139 "kernelspec": {
140 "display_name": "Ruby 2.4.0",
141 "language": "ruby",
142 "name": "ruby"
143 },
144 "language_info": {
145 "file_extension": ".rb",
146 "mimetype": "application/x-ruby",
147 "name": "ruby",
148 "version": "2.4.0"
149 }
150 },
151 "nbformat": 4,
152 "nbformat_minor": 2
153 }