Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q31\n", "How many different ways can £2 be made using any number of coins?" ] }, { "cell_type": "code", "execution_count": 1, "id": "019a77e5-ff2f-4124-a105-b452fcb4a385", "metadata": {}, "outputs": [], "source": [ "def vector_add(x_vector,i,v):\n", " x_vector[i] = v\n", " return x_vector\n", "\n", "def count_coin_comb(need,coin_i=0,coin_counts=[0,0,0,0,0,0,0,0],coin_faces=[1,2,5,10,20,50,100,200]):\n", " return ( sum( 1 for p in range(0,need//coin_faces[coin_i]+1) \n", " if need-coin_faces[coin_i]*p==0\n", " ) if coin_i >= len(coin_faces)-1 else \n", " sum( count_coin_comb(need-coin_faces[coin_i]*p,\n", " coin_i+1,\n", " vector_add(coin_counts,coin_i,p)) \n", " for p in range(0,need//coin_faces[coin_i]+1)\n", " )\n", " ) " ] }, { "cell_type": "code", "execution_count": 2, "id": "ebe949d2-ea8f-4b32-ab58-7dc914e8cc8f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4.4 s, sys: 0 ns, total: 4.4 s\n", "Wall time: 4.4 s\n" ] }, { "data": { "text/plain": [ "73682" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "count_coin_comb(200)" ] }, { "cell_type": "code", "execution_count": 3, "id": "ab0e2c98-cc97-482d-8c3a-a87c2d9a39df", "metadata": {}, "outputs": [], "source": [ "def count_coin_comb_1(need,coin_faces=[0,1,2,5,10,20,50,100,200]):\n", " num_coin_type = len(coin_faces)\n", " result_matrix = [[0 for j in range(need+1)] for i in range(num_coin_type)]\n", " for i in range(1,need+1):\n", " result_matrix[1][i] = 1\n", " for i in range(1,num_coin_type):\n", " result_matrix[i][0] = 1\n", " for i in range(1,num_coin_type):\n", " for j in range(1,need+1):\n", " for k in range(j//coin_faces[i]+1):\n", " result_matrix[i][j]+=result_matrix[i-1][j-k*coin_faces[i]];\n", " return result_matrix[-1][-1]\n", " " ] }, { "cell_type": "code", "execution_count": 4, "id": "30cff9f4-b558-4b7c-9bb8-0cfa752d2567", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4.93 ms, sys: 0 ns, total: 4.93 ms\n", "Wall time: 4.72 ms\n" ] }, { "data": { "text/plain": [ "73682" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "count_coin_comb_1(200)" ] }, { "cell_type": "code", "execution_count": null, "id": "48b04317-734e-4c7f-af25-e66513026c05", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q30
Next Notebook:
Project Euler Q32
Loading