eulerbooks/notebooks/problem0069.ipynb

84 lines
2.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "d4c97c0c",
"metadata": {},
"source": [
"# [Totient Maximum](https://projecteuler.net/problem=69)\n",
"\n",
"This problem can be solved by hand.\n",
"\n",
"The [totient function](https://en.wikipedia.org/wiki/Euler%27s_totient_function) can be computed as\n",
"$$\\phi(n) = n\\prod_{p|n} \\left(1 - \\frac{1}{p}\\right)$$\n",
"Therefore\n",
"$$\\frac{n}{\\phi(n)} = \\prod_{p|n}\\left(\\frac{p}{p-1}\\right)$$\n",
"This implies that $n/\\phi(n)$ increases as the number of distinct prime factors of $n$ increases, so to maximize this quotient, we want $n$ to have as many distinct prime factors as possible.\n",
"\n",
"Furthermore, if $p < q$ for primes $p$ and $q$, then\n",
"$$\\frac{p}{p-1} > \\frac{q}{q-1}$$\n",
"Put simply, this means that smaller prime factors will lead to a larger $n/\\phi(n)$. These two facts suggest we should try numbers that are the product of the first several primes, which are called [primorials](https://en.wikipedia.org/wiki/Primorial). Our answer then is the largest primorial less than 1000000:\n",
"$$2 \\times 3 \\times 5 \\times 7 \\times 11 \\times 13 \\times 17 = 510510$$\n",
"\n",
"Alternatively, you can just ask SageMath - its implementation of $\\phi(n)$ is fast enough to brute force this problem. However, you might run into difficulties writing your own implementation that's this fast."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "da203e05",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"510510"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max(range(1, 1000001), key=lambda n: n / euler_phi(n))"
]
},
{
"cell_type": "markdown",
"id": "5d99957e",
"metadata": {},
"source": [
"## Relevant sequences\n",
"* Totient: [A000010](https://oeis.org/A000010)\n",
"* Primorial numbers: [A002110](https://oeis.org/A002110)\n",
"\n",
"#### Copyright (C) 2025 filifa\n",
"\n",
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."
]
}
],
"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
}