From 9b41388ea9975caa337cec7e2b9abb7a01fe8b72 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 28 Apr 2017 19:20:02 +0100 Subject: [PATCH] Done puzzle 62 --- euler62.ipynb | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 euler62.ipynb diff --git a/euler62.ipynb b/euler62.ipynb new file mode 100644 index 0000000..cf2d377 --- /dev/null +++ b/euler62.ipynb @@ -0,0 +1,153 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "true" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "require_relative 'array-numbers'" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "865" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cubes = Hash.new {|h, k| h[k] = []}\n", + "cube_roots = {}\n", + "(100..1000).each do |i|\n", + " c = (i ** 3)\n", + " p = c.to_digits.sort.join\n", + " # cubes[p] = (cubes[p] + [c])\n", + " cubes[p] << c\n", + " cube_roots[c] = i\n", + "end\n", + "cubes.length" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{\"01234566\"=>[41063625, 56623104, 66430125]}" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cubes.select {|k, v| v.length >= 3}" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "345" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cube_roots[41063625]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "127035954683" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "target = 5\n", + "\n", + "cubes = Hash.new {|h, k| h[k] = []}\n", + "finished = false\n", + "i = 100\n", + "while ! finished\n", + " c = (i ** 3)\n", + " p = c.to_digits.sort.join\n", + " cubes[p] << c\n", + " if cubes[p].length == target\n", + " finished = true\n", + " else\n", + " i += 1\n", + " end\n", + "end\n", + "\n", + "cubes[p].min" + ] + }, + { + "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": 2 +} -- 2.34.1