Done puzzle 64
[project-euler.git] / euler41.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": 17,
29 "metadata": {
30 "collapsed": false
31 },
32 "outputs": [
33 {
34 "data": {
35 "text/plain": [
36 ":pandigital?"
37 ]
38 },
39 "execution_count": 17,
40 "metadata": {},
41 "output_type": "execute_result"
42 }
43 ],
44 "source": [
45 "class Integer\n",
46 " def pandigital?\n",
47 " self.to_digits.sort == (1..Math.log10(self).ceil).to_a\n",
48 " end\n",
49 "end"
50 ]
51 },
52 {
53 "cell_type": "code",
54 "execution_count": 6,
55 "metadata": {
56 "collapsed": false
57 },
58 "outputs": [
59 {
60 "data": {
61 "text/plain": [
62 "2"
63 ]
64 },
65 "execution_count": 6,
66 "metadata": {},
67 "output_type": "execute_result"
68 }
69 ],
70 "source": [
71 "Math.log10(21).ceil"
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": 13,
77 "metadata": {
78 "collapsed": false
79 },
80 "outputs": [
81 {
82 "data": {
83 "text/plain": [
84 "[[12, [1, 2], [1, 2]], [21, [1, 2], [1, 2]], [123, [1, 2, 3], [1, 2, 3]], [132, [1, 2, 3], [1, 2, 3]], [213, [1, 2, 3], [1, 2, 3]], [231, [1, 2, 3], [1, 2, 3]], [312, [1, 2, 3], [1, 2, 3]], [321, [1, 2, 3], [1, 2, 3]], [1234, [1, 2, 3, 4], [1, 2, 3, 4]], [1243, [1, 2, 3, 4], [1, 2, 3, 4]], [1324, [1, 2, 3, 4], [1, 2, 3, 4]], [1342, [1, 2, 3, 4], [1, 2, 3, 4]], [1423, [1, 2, 3, 4], [1, 2, 3, 4]], [1432, [1, 2, 3, 4], [1, 2, 3, 4]]]"
85 ]
86 },
87 "execution_count": 13,
88 "metadata": {},
89 "output_type": "execute_result"
90 }
91 ],
92 "source": [
93 "(1..2000).map {|i| [i, i.to_digits.sort, (1..Math.log10(i).ceil).to_a]}.select {|i, ds, ms| ds == ms}"
94 ]
95 },
96 {
97 "cell_type": "code",
98 "execution_count": 19,
99 "metadata": {
100 "collapsed": false
101 },
102 "outputs": [
103 {
104 "data": {
105 "text/plain": [
106 "false"
107 ]
108 },
109 "execution_count": 19,
110 "metadata": {},
111 "output_type": "execute_result"
112 }
113 ],
114 "source": [
115 "13.pandigital?"
116 ]
117 },
118 {
119 "cell_type": "markdown",
120 "metadata": {},
121 "source": [
122 "# Divisibility rule\n",
123 "A number is divisible by 3 iff the sum of its digits is divisible by 3. Therefore, all pandigital numbers, apart from those to 4 and 7, are composite. \n",
124 "\n",
125 "Therefore, we check just the seven digit primes first."
126 ]
127 },
128 {
129 "cell_type": "code",
130 "execution_count": 30,
131 "metadata": {
132 "collapsed": false
133 },
134 "outputs": [
135 {
136 "data": {
137 "text/plain": [
138 "7652413"
139 ]
140 },
141 "execution_count": 30,
142 "metadata": {},
143 "output_type": "execute_result"
144 }
145 ],
146 "source": [
147 "(1..7).to_a.permutation.select {|ds| ds.to_i.prime?}.map {|ds| ds.to_i}.max"
148 ]
149 },
150 {
151 "cell_type": "code",
152 "execution_count": null,
153 "metadata": {
154 "collapsed": true
155 },
156 "outputs": [],
157 "source": []
158 }
159 ],
160 "metadata": {
161 "kernelspec": {
162 "display_name": "Ruby 2.4.0",
163 "language": "ruby",
164 "name": "ruby"
165 },
166 "language_info": {
167 "file_extension": ".rb",
168 "mimetype": "application/x-ruby",
169 "name": "ruby",
170 "version": "2.4.0"
171 }
172 },
173 "nbformat": 4,
174 "nbformat_minor": 0
175 }