use a generator

This commit is contained in:
filifa 2025-06-28 20:38:49 -04:00
parent a37a039dfe
commit 331a444bc3
1 changed files with 28 additions and 19 deletions

View File

@ -32,51 +32,60 @@
"As in [problem 69](https://projecteuler.net/problem=69),\n", "As in [problem 69](https://projecteuler.net/problem=69),\n",
"$$\\phi(n) = n\\prod_{p | n} \\left(1 - \\frac{1}{p}\\right)$$\n", "$$\\phi(n) = n\\prod_{p | n} \\left(1 - \\frac{1}{p}\\right)$$\n",
"\n", "\n",
"We can calculate the totients of the numbers up to $10^7$ using a very similar approach to the [sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) for generating prime numbers (see [problem 10](https://projecteuler.net/problem=10)).\n", "We can calculate the totients of the numbers up to $10^7$ using a very similar approach to the [sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) for generating prime numbers (see [problem 10](https://projecteuler.net/problem=10)). Iterating over values of $n$, if `totients[n] == n - 1`, then $n$ is prime, and we'll update all its multiples using the above formula."
"\n",
"We'll initialize a list of numbers from 0 to $10^7$."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"id": "524fca40", "id": "54065139",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"limit = 10^7\n", "def totients(limit):\n",
"totients = list(range(0, limit))" " totients = [n - 1 for n in range(0, limit)]\n",
" totients[0] = 0\n",
" totients[1] = 1\n",
" \n",
" for n in range(0, limit // 2 + 1):\n",
" yield totients[n]\n",
" if n == 0 or n == 1 or totients[n] != n - 1:\n",
" continue\n",
"\n",
" for k in range(2 * n, limit, n):\n",
" totients[k] -= totients[k] // n\n",
" \n",
" yield from totients[limit // 2 + 1:]"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "88ef58dd", "id": "45a80c4b",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Iterating $n$ from 2 to $10^7$, if `totients[n] == n`, then $n$ is prime, and we'll update its totient and all its multiples using the above formula. If `totients[n] != n`, then we'll check if $n/\\phi(n)$ is small and if $\\phi(n)$ is a permutation of $n$, keeping track of the best answer so far." "$n-1$ can't be a permutation of $n$, so our solution won't be prime. If $n$ is composite, then we'll check if $n/\\phi(n)$ is small and if $\\phi(n)$ is a permutation of $n$, keeping track of the best answer so far."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"id": "aa071cc4", "id": "60741588",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"limit = 10^7\n",
"\n",
"answer = None\n", "answer = None\n",
"ratio = float('inf')\n", "ratio = float('inf')\n",
"\n", "\n",
"for n in range(2, limit):\n", "for (n, totient) in enumerate(totients(limit)):\n",
" if totients[n] != n:\n", " if n == 0 or n == 1 or totient == n - 1:\n",
" r = n / totients[n]\n",
" if r < ratio and is_permutation_pair(n, totients[n]):\n",
" ratio = r\n",
" answer = n\n",
"\n",
" continue\n", " continue\n",
"\n", " \n",
" for p in range(n, limit, n):\n", " r = n / totient\n",
" totients[p] -= totients[p] // n" " if r < ratio and is_permutation_pair(n, totient):\n",
" ratio = r\n",
" answer = n"
] ]
}, },
{ {