add problem 53
This commit is contained in:
parent
cba16eb5d1
commit
cb12d06321
|
@ -0,0 +1,78 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "59fa6654",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# [Combinatoric Selections](https://projecteuler.net/problem=53)\n",
|
||||
"\n",
|
||||
"Python/SageMath's [unlimited precision integers](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex) make this trivial."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "abf93cda",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"4075"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"binoms = (binomial(n, r) for n in range(1, 101) for r in range(0, n + 1))\n",
|
||||
"len([b for b in binoms if b > 1000000])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6d7f4409",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If it weren't for unlimited precision, we would have a problem, since a lot of these [binomial coefficients](https://en.wikipedia.org/wiki/Binomial_coefficient) overflow a 64 bit integer - the largest one we encounter here is $\\binom{100}{50} = 100891344545564193334812497256$.\n",
|
||||
"\n",
|
||||
"However, even in that case there's a pretty simple workaround: finding $n$ and $r$ such that\n",
|
||||
"$$\\binom{n}{r} > 1000000$$\n",
|
||||
"is the same as finding $n$ and $r$ such that\n",
|
||||
"$$\\log{\\binom{n}{r}} > \\log{1000000}$$\n",
|
||||
"By applying the definition of the binomial coefficients and [logarithmic identities](https://en.wikipedia.org/wiki/List_of_logarithmic_identities), this turns to\n",
|
||||
"$$\\log{n!} - \\log{r!} - \\log{(n-r)!} > \\log{1000000}$$\n",
|
||||
"$\\log{100!} \\approx 363.739$, nowhere close to overflowing a float.\n",
|
||||
"\n",
|
||||
"$\\log{n!}$ can be computed in a number of ways. You can use the [log-gamma function](https://en.wikipedia.org/wiki/Gamma_function), or you can implement a function yourself, either by applying logarithmic identities again:\n",
|
||||
"$$\\log{n!} = \\log{1} + \\log{2} + \\log{3} \\cdots + \\log{n}$$\n",
|
||||
"or by using [Stirling's approximation](https://en.wikipedia.org/wiki/Stirling%27s_approximation):\n",
|
||||
"$$\\log{n!} \\approx \\left(n + \\frac{1}{2}\\right)\\log{n} - n + \\frac{1}{2}\\log{2\\pi}$$"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "SageMath 9.5",
|
||||
"language": "sage",
|
||||
"name": "sagemath"
|
||||
},
|
||||
"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.11.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Loading…
Reference in New Issue