From a30659fc716baee25a4f543820b0018b48df89f2 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 17 Mar 2017 17:25:36 +0000 Subject: [PATCH] Done puzzle 49 --- euler49.ipynb | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 euler49.ipynb diff --git a/euler49.ipynb b/euler49.ipynb new file mode 100644 index 0000000..b9cf0a6 --- /dev/null +++ b/euler49.ipynb @@ -0,0 +1,180 @@ +{ + "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": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1234.to_digits" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":valid" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def valid(n, debug=false)\n", + " digits = n.to_digits\n", + " perms = digits.permutation.select {|ds| ds.to_i.prime?}.to_set.to_a.permutation(3)\n", + " puts \"perms: #{perms}\" if debug\n", + " triples = perms.to_a.map {|t| t.map {|ds| ds.to_i}}\n", + " puts \"triples: #{triples}\" if debug\n", + " valids = triples.select {|t| t[0] == n && t[1] > t[0] && t[1] - t[0] == t[2] - t[1]}\n", + " puts \"valids: #{valids}\" if debug\n", + " valids[0]\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "false" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "valid(1234)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[148748178147, 296962999629]" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1000..9999).map {|n| valid n}.select {|t| !t.nil?}.map {|t| t.flat_map {|n| n.to_digits}.to_i}" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "perms: #\n", + "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", + "valids: [[2969, 6299, 9629]]\n" + ] + }, + { + "data": { + "text/plain": [ + "[2969, 6299, 9629]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "valid(2969, debug=true)" + ] + }, + { + "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 +} -- 2.34.1