{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Euler Problem 31\n", "\n", "In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:\n", "\n", "1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).\n", "\n", "It is possible to make £2 in the following way:\n", "\n", "1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p\n", "\n", "How many different ways can £2 be made using any number of coins?" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Let's do it non-recursive. Tricky part is to use ceil, otherwise we get wrong ranges for example for $\\frac{50}{20}$." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1]\n", "[0, 1, 2]\n" ] } ], "source": [ "from math import ceil\n", "\n", "print(list(range(50 // 20)))\n", "print(list(range(ceil(50 / 20))))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from math import ceil\n", "\n", "r = 200\n", "c = 0\n", "\n", "if r % 200 == 0:\n", " c += 1\n", "for p200 in range(ceil(r / 200)):\n", " r200 = r - p200 * 200\n", " if r200 % 100 == 0:\n", " c += 1\n", " for p100 in range(ceil(r200 / 100)):\n", " r100 = r200 - p100 * 100\n", " if r100 % 50 == 0:\n", " c += 1\n", " for p50 in range(ceil(r100 / 50)):\n", " r50 = r100 - p50 * 50\n", " if r50 % 20 == 0:\n", " c += 1\n", " for p20 in range(ceil(r50 / 20)):\n", " r20 = r50 - p20 * 20\n", " if r20 <= 0:\n", " break\n", " if r20 % 10 == 0:\n", " c += 1\n", " for p10 in range(ceil(r20 / 10)):\n", " r10 = r20 - p10 * 10\n", " if r10 % 5 == 0:\n", " c += 1\n", " for p5 in range(ceil(r10 / 5)):\n", " r5 = r10 - p5 * 5\n", " if r5 % 2 == 0:\n", " c += 1\n", " for p2 in range(ceil(r5 / 2)):\n", " r2 = r5 - p2 * 2\n", " if r2 % 1 == 0:\n", " c += 1\n", "\n", "s = c" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "73682\n" ] } ], "source": [ "print(s)\n", "assert(s == 73682)" ] } ], "metadata": { "completion_date": "Fri, 25 Aug 2017, 13:02", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.4" }, "tags": [ "recursion", "coins" ] }, "nbformat": 4, "nbformat_minor": 2 }