add problem 65
This commit is contained in:
parent
4f8da514cc
commit
b90b6d1e4c
|
@ -0,0 +1,137 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "bdec47c4",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# [Convergents of $e$](https://projecteuler.net/problem=65)\n",
|
||||||
|
"\n",
|
||||||
|
"Easy one-liner in SageMath."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "8a93028c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"272"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"sum(continued_fraction(e).convergent(99).numerator().digits())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "396468b7",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"If we wanted to compute the convergents ourselves, we could first make a generator for the partial denominators of the continued fraction of $e$."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "bedc34c8",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from itertools import count, chain\n",
|
||||||
|
"\n",
|
||||||
|
"def partial_denominators_e(n):\n",
|
||||||
|
" yield 2\n",
|
||||||
|
" denominators = chain.from_iterable((1, 2 * k, 1) for k in count(1))\n",
|
||||||
|
" for (i, b) in enumerate(denominators):\n",
|
||||||
|
" if i >= n:\n",
|
||||||
|
" break\n",
|
||||||
|
" \n",
|
||||||
|
" yield b"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "33288cd0",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Then write a function for computing a continued fraction from a sequence of partial denominators (outside of SageMath, you might want to use a [fraction type](https://docs.python.org/3/library/fractions.html))."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "1f7bab23",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def cf(denominators):\n",
|
||||||
|
" a = next(denominators)\n",
|
||||||
|
" \n",
|
||||||
|
" try:\n",
|
||||||
|
" return a + 1 / cf(denominators)\n",
|
||||||
|
" except StopIteration:\n",
|
||||||
|
" return a"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "5b8189b7",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"272"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"sum(cf(partial_denominators_e(99)).numerator().digits())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "5778175d",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Relevant sequences\n",
|
||||||
|
"* Numerators of convergents of $e$: [A007676](https://oeis.org/A007676)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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