Done puzzle 49
[project-euler.git] / euler49.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [
10 {
11 "data": {
12 "text/plain": [
13 "true"
14 ]
15 },
16 "execution_count": 1,
17 "metadata": {},
18 "output_type": "execute_result"
19 }
20 ],
21 "source": [
22 "load 'primes.rb'\n",
23 "load 'array-numbers.rb'"
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": 13,
29 "metadata": {
30 "collapsed": false
31 },
32 "outputs": [
33 {
34 "data": {
35 "text/plain": [
36 "[1, 2, 3, 4]"
37 ]
38 },
39 "execution_count": 13,
40 "metadata": {},
41 "output_type": "execute_result"
42 }
43 ],
44 "source": [
45 "1234.to_digits"
46 ]
47 },
48 {
49 "cell_type": "code",
50 "execution_count": 53,
51 "metadata": {
52 "collapsed": false
53 },
54 "outputs": [
55 {
56 "data": {
57 "text/plain": [
58 ":valid"
59 ]
60 },
61 "execution_count": 53,
62 "metadata": {},
63 "output_type": "execute_result"
64 }
65 ],
66 "source": [
67 "def valid(n, debug=false)\n",
68 " digits = n.to_digits\n",
69 " perms = digits.permutation.select {|ds| ds.to_i.prime?}.to_set.to_a.permutation(3)\n",
70 " puts \"perms: #{perms}\" if debug\n",
71 " triples = perms.to_a.map {|t| t.map {|ds| ds.to_i}}\n",
72 " puts \"triples: #{triples}\" if debug\n",
73 " valids = triples.select {|t| t[0] == n && t[1] > t[0] && t[1] - t[0] == t[2] - t[1]}\n",
74 " puts \"valids: #{valids}\" if debug\n",
75 " valids[0]\n",
76 "end"
77 ]
78 },
79 {
80 "cell_type": "code",
81 "execution_count": 35,
82 "metadata": {
83 "collapsed": false
84 },
85 "outputs": [
86 {
87 "data": {
88 "text/plain": [
89 "false"
90 ]
91 },
92 "execution_count": 35,
93 "metadata": {},
94 "output_type": "execute_result"
95 }
96 ],
97 "source": [
98 "valid(1234)"
99 ]
100 },
101 {
102 "cell_type": "code",
103 "execution_count": 56,
104 "metadata": {
105 "collapsed": false
106 },
107 "outputs": [
108 {
109 "data": {
110 "text/plain": [
111 "[148748178147, 296962999629]"
112 ]
113 },
114 "execution_count": 56,
115 "metadata": {},
116 "output_type": "execute_result"
117 }
118 ],
119 "source": [
120 "(1000..9999).map {|n| valid n}.select {|t| !t.nil?}.map {|t| t.flat_map {|n| n.to_digits}.to_i}"
121 ]
122 },
123 {
124 "cell_type": "code",
125 "execution_count": 57,
126 "metadata": {
127 "collapsed": false,
128 "scrolled": true
129 },
130 "outputs": [
131 {
132 "name": "stdout",
133 "output_type": "stream",
134 "text": [
135 "perms: #<Enumerator:0x005594b994f0e0>\n",
136 "triples: [[2969, 2699, 9629], [2969, 2699, 6299], [2969, 9629, 2699], [2969, 9629, 6299], [2969, 6299, 2699], [2969, 6299, 9629], [2699, 2969, 9629], [2699, 2969, 6299], [2699, 9629, 2969], [2699, 9629, 6299], [2699, 6299, 2969], [2699, 6299, 9629], [9629, 2969, 2699], [9629, 2969, 6299], [9629, 2699, 2969], [9629, 2699, 6299], [9629, 6299, 2969], [9629, 6299, 2699], [6299, 2969, 2699], [6299, 2969, 9629], [6299, 2699, 2969], [6299, 2699, 9629], [6299, 9629, 2969], [6299, 9629, 2699]]\n",
137 "valids: [[2969, 6299, 9629]]\n"
138 ]
139 },
140 {
141 "data": {
142 "text/plain": [
143 "[2969, 6299, 9629]"
144 ]
145 },
146 "execution_count": 57,
147 "metadata": {},
148 "output_type": "execute_result"
149 }
150 ],
151 "source": [
152 "valid(2969, debug=true)"
153 ]
154 },
155 {
156 "cell_type": "code",
157 "execution_count": null,
158 "metadata": {
159 "collapsed": true
160 },
161 "outputs": [],
162 "source": []
163 }
164 ],
165 "metadata": {
166 "kernelspec": {
167 "display_name": "Ruby 2.4.0",
168 "language": "ruby",
169 "name": "ruby"
170 },
171 "language_info": {
172 "file_extension": ".rb",
173 "mimetype": "application/x-ruby",
174 "name": "ruby",
175 "version": "2.4.0"
176 }
177 },
178 "nbformat": 4,
179 "nbformat_minor": 0
180 }