euler/ipython/EulerProblem031.ipynb

149 lines
3.5 KiB
Plaintext
Raw Normal View History

2018-02-09 16:39:51 +01:00
{
"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",
2018-02-09 16:39:51 +01:00
"metadata": {
"collapsed": true
},
"source": [
2018-02-11 16:26:30 +01:00
"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",
2018-02-11 16:26:30 +01:00
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2018-02-11 16:26:30 +01:00
"[0, 1]\n",
"[0, 1, 2]\n"
]
}
],
"source": [
2018-02-11 16:26:30 +01:00
"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",
2018-02-11 16:26:30 +01:00
"for p200 in range(ceil(r / 200)):\n",
" r200 = r - p200 * 200\n",
" if r200 % 100 == 0:\n",
" c += 1\n",
2018-02-11 16:26:30 +01:00
" for p100 in range(ceil(r200 / 100)):\n",
" r100 = r200 - p100 * 100\n",
" if r100 % 50 == 0:\n",
" c += 1\n",
2018-02-11 16:26:30 +01:00
" for p50 in range(ceil(r100 / 50)):\n",
" r50 = r100 - p50 * 50\n",
" if r50 % 20 == 0:\n",
" c += 1\n",
2018-02-11 16:26:30 +01:00
" 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",
2018-02-11 16:26:30 +01:00
" for p10 in range(ceil(r20 / 10)):\n",
" r10 = r20 - p10 * 10\n",
" if r10 % 5 == 0:\n",
" c += 1\n",
2018-02-11 16:26:30 +01:00
" for p5 in range(ceil(r10 / 5)):\n",
" r5 = r10 - p5 * 5\n",
" if r5 % 2 == 0:\n",
" c += 1\n",
2018-02-11 16:26:30 +01:00
" for p2 in range(ceil(r5 / 2)):\n",
" r2 = r5 - p2 * 2\n",
2018-02-11 16:26:30 +01:00
" if r2 % 1 == 0:\n",
" c += 1\n",
"\n",
2018-02-11 16:26:30 +01:00
"s = c"
]
},
{
"cell_type": "code",
2018-02-11 16:26:30 +01:00
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2018-02-11 16:26:30 +01:00
"73682\n"
]
}
],
"source": [
"print(s)\n",
2018-02-11 16:26:30 +01:00
"assert(s == 73682)"
2018-02-09 16:39:51 +01:00
]
}
],
"metadata": {
"completion_date": "Fri, 25 Aug 2017, 13:02",
2018-02-09 16:39:51 +01:00
"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"
]
2018-02-09 16:39:51 +01:00
},
"nbformat": 4,
"nbformat_minor": 2
}