From: Neil Smith Date: Tue, 21 Feb 2017 16:48:13 +0000 (+0000) Subject: Done puzzle 41 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=41e688ebef5be3004fa0c13196812711b9708043;p=project-euler.git Done puzzle 41 --- diff --git a/euler41.ipynb b/euler41.ipynb new file mode 100644 index 0000000..82f8791 --- /dev/null +++ b/euler41.ipynb @@ -0,0 +1,175 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "true" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "load 'primes.rb'\n", + "load 'array-numbers.rb'" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":pandigital?" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class Integer\n", + " def pandigital?\n", + " self.to_digits.sort == (1..Math.log10(self).ceil).to_a\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Math.log10(21).ceil" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[[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]]]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1..2000).map {|i| [i, i.to_digits.sort, (1..Math.log10(i).ceil).to_a]}.select {|i, ds, ms| ds == ms}" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "false" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "13.pandigital?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Divisibility rule\n", + "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", + "\n", + "Therefore, we check just the seven digit primes first." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7652413" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1..7).to_a.permutation.select {|ds| ds.to_i.prime?}.map {|ds| ds.to_i}.max" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Ruby 2.4.0", + "language": "ruby", + "name": "ruby" + }, + "language_info": { + "file_extension": ".rb", + "mimetype": "application/x-ruby", + "name": "ruby", + "version": "2.4.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}