From: Neil Smith Date: Wed, 5 Apr 2017 09:38:59 +0000 (+0100) Subject: Done puzzle 53 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=ef1db8cb491b54c8e54b857d7f70e159917cce4f;p=project-euler.git Done puzzle 53 --- diff --git a/euler53.ipynb b/euler53.ipynb new file mode 100644 index 0000000..823e3b2 --- /dev/null +++ b/euler53.ipynb @@ -0,0 +1,116 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Integer\n", + " def factorial\n", + " (1..self).inject(:*) || 1\n", + " end\n", + " alias :! :factorial\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1..100).each do |n|\n", + " (1..n).each do |r|\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1..10).to_a.product((1..10).to_a).select {|n, r| n >= r}.map {|n, r| n.! / (r.! * (n - r).!)}.select {|c| c> 100}.length" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4075" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1..100).to_a.product((1..100).to_a).select {|n, r| \n", + " n >= r}.map {|n, r| \n", + " n.! / (r.! * (n - r).!)}.select {|c| \n", + " c > 10 ** 6}.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 +}