eulerbooks/notebooks/problem0072.ipynb

76 lines
2.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "b6aef7e5",
"metadata": {},
"source": [
"# [Counting Fractions](https://projecteuler.net/problem=72)\n",
"\n",
"Like [problem 71](https://projecteuler.net/problem=71), we're looking at [Farey sequences](https://en.wikipedia.org/wiki/Farey_sequence). This time we're interested in the cardinality of $F_{1000000}$.\n",
"\n",
"To begin, first note that $F_1 = \\{0, 1\\}$, so $|F_1| = 2$ (this problem isn't counting 0 and 1 in its totals - we'll handle that at the end). Then consider that for any Farey sequence $F_n$, the next sequence $F_{n+1}$ will contain all the terms from $F_n$, along with all irreducible fractions $\\frac{k}{n+1}$, (since any *reducible* fraction would already be in $F_n$).\n",
"\n",
"How many new fractions does this get us? Well, the fraction only reduces if $k$ and $n+1$ have a common factor - in other words, if $k$ and $n+1$ are coprime, the fraction will not reduce. How many number less than $n+1$ are coprime to $n+1$? The [totient function](https://en.wikipedia.org/wiki/Euler%27s_totient_function) will tell us! So the number of irreducible fractions with denominator $n+1$ is simply $\\phi(n+1)$ This gives us\n",
"$$|F_{n+1}| = |F_n| + \\phi(n+1)$$\n",
"From this, we can derive a non-recursive formula:\n",
"$$|F_n| = 1 + \\sum_{k=1}^n \\phi(k)$$\n",
"\n",
"As mentioned before, we'll actually subtract two from this total, since the problem isn't counting 0 or 1."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2d21b0a4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"303963552391"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(euler_phi(n) for n in range(1, 1000001)) - 1"
]
},
{
"cell_type": "markdown",
"id": "1c67d8da",
"metadata": {},
"source": [
"## Relevant sequences\n",
"* Cardinalities of Farey sequences: [A005728](https://oeis.org/A005728)\n",
"* Partial sums of totient function: [A002088](https://oeis.org/A002088)"
]
}
],
"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
}