From 5d889313d96683e6a88ad3e3a9cd51c5bc88d2a5 Mon Sep 17 00:00:00 2001 From: filifa Date: Thu, 22 May 2025 20:11:38 -0400 Subject: [PATCH] add problem 69 --- notebooks/problem0069.ipynb | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 notebooks/problem0069.ipynb diff --git a/notebooks/problem0069.ipynb b/notebooks/problem0069.ipynb new file mode 100644 index 0000000..f1ac654 --- /dev/null +++ b/notebooks/problem0069.ipynb @@ -0,0 +1,80 @@ +{ + "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 for 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": [ + "qs = {n: n/euler_phi(n) for n in range(1, 1000001)}\n", + "max(qs, key=qs.get)" + ] + }, + { + "cell_type": "markdown", + "id": "5d99957e", + "metadata": {}, + "source": [ + "## Relevant sequences\n", + "* Totient: [A000010](https://oeis.org/A000010)\n", + "* Primorial numbers: [A002110](https://oeis.org/A002110)" + ] + } + ], + "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 +}