From ba439dd4696eaf001ec1464d27d6a714b4c54bbe Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 17 Feb 2017 18:08:25 +0000 Subject: [PATCH] Done puzzle 39 --- euler39.ipynb | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 euler39.ipynb diff --git a/euler39.ipynb b/euler39.ipynb new file mode 100644 index 0000000..47d07f3 --- /dev/null +++ b/euler39.ipynb @@ -0,0 +1,127 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "false" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "require 'set'" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1..1000" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "results = Hash.new {|x| Set.new}\n", + "(1..1000).each do |p|\n", + " (1..(p/3)).each do |a|\n", + " (1..((p-a)/2)).each do |b|\n", + " c = p-(a+b)\n", + " if a ** 2 + b ** 2 == c ** 2\n", + " # puts \"p = #{p}, a, b, c: #{a} #{b} #{c}\"\n", + " results[p] = results[p].add(Set.new [a, b, c])\n", + " end\n", + " end\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "results[120].length" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "840" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "results.keys.max_by {|p| results[p].length}" + ] + }, + { + "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