From: Neil Smith Date: Fri, 17 Mar 2017 17:25:31 +0000 (+0000) Subject: Done puzzle 48 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=31adaa8c6015dd5cd2d70b3b21813c40820f962e;p=project-euler.git Done puzzle 48 --- diff --git a/euler48.ipynb b/euler48.ipynb new file mode 100644 index 0000000..0497bf6 --- /dev/null +++ b/euler48.ipynb @@ -0,0 +1,149 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":modular_exponent" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Calculate ( n ** e ) mod b\n", + "def modular_exponent(n, e, b)\n", + " e_prime = 1\n", + " c = n % b\n", + " while e_prime < e\n", + " c = (c * n) % b\n", + " e_prime += 1\n", + " end\n", + " c\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "modular_exponent 4, 3, 10" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":self_powers" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def self_powers(n_max, b)\n", + " (1..n_max).map {|n| modular_exponent n, n, b}.sum % b\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10405071317" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "self_powers 10, 1000000000000" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "9110846700" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "self_powers 1000, 10**10" + ] + }, + { + "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 +}