show the easy way with SageMath

This commit is contained in:
filifa
2025-05-24 23:34:48 -04:00
parent 6e9f787eb5
commit c5622fc968
2 changed files with 80 additions and 15 deletions

View File

@@ -1,13 +1,42 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e5793a3a",
"metadata": {},
"source": [
"# [Coin Sums](https://projecteuler.net/problem=31)\n",
"\n",
"The easiest thing to do here is... let SageMath do the whole thing for us."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9bedec84",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"73682"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Partitions(200, parts_in=(1,2,5,10,20,50,100,200)).cardinality()"
]
},
{
"cell_type": "markdown",
"id": "44f7d7da",
"metadata": {},
"source": [
"# [Coin Sums](https://projecteuler.net/problem=31)\n",
"\n",
"There's a *very* cool way to solve this problem using [generating functions](https://en.wikipedia.org/wiki/Generating_function).\n",
"But that's boring, and there's a *very* cool way to solve this problem using [generating functions](https://en.wikipedia.org/wiki/Generating_function).\n",
"\n",
"## Generating functions\n",
"If you're not familiar, a generating function is a way of expressing a sequence as coefficients of a [formal power series](https://en.wikipedia.org/wiki/Formal_power_series). For example, the generating function for the Fibonacci sequence is\n",
@@ -24,7 +53,7 @@
"\n",
"Similarly, how many ways can you make 0 cents, 1 cent, 2 cents, etc. using only 2 cent coins? Answer: every even value has one way of making change, and every odd value has zero ways (1, 0, 1, 0, 1, 0, ...). Similarly, with only 5 cent coins, there is one way to make change for every multiple of 5, and zero ways for every other value (1, 0, 0, 0, 0, 1, ...).\n",
"\n",
"We can express the solutions to each of these questions as sequences, and therefore as generating functions:\n",
"We can express the answers to each of these questions as sequences, and therefore as generating functions:\n",
"$$\\begin{align}\n",
"1 + x + x^2 + x^3 + \\cdots = \\frac{1}{1-x}\\\\\n",
"1 + x^2 + x^4 + x^6 + \\cdots = \\frac{1}{1-x^2}\\\\\n",
@@ -47,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "74c5fe00",
"metadata": {},
"outputs": [
@@ -57,7 +86,7 @@
"x^18 + x^17 + 2*x^16 + 2*x^15 + 3*x^14 + 4*x^13 + 5*x^12 + 6*x^11 + 5*x^10 + 6*x^9 + 5*x^8 + 6*x^7 + 5*x^6 + 4*x^5 + 3*x^4 + 2*x^3 + 2*x^2 + x + 1"
]
},
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@@ -106,7 +135,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "c351c248",
"metadata": {},
"outputs": [
@@ -116,7 +145,7 @@
"73682.0"
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@@ -149,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "33bbc1a4",
"metadata": {},
"outputs": [
@@ -159,7 +188,7 @@
"73682"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -179,7 +208,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "c2a5db7e",
"metadata": {},
"outputs": [
@@ -189,7 +218,7 @@
"73682"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}