use a generator
This commit is contained in:
parent
a37a039dfe
commit
331a444bc3
|
@ -32,51 +32,60 @@
|
|||
"As in [problem 69](https://projecteuler.net/problem=69),\n",
|
||||
"$$\\phi(n) = n\\prod_{p | n} \\left(1 - \\frac{1}{p}\\right)$$\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",
|
||||
"\n",
|
||||
"We'll initialize a list of numbers from 0 to $10^7$."
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "524fca40",
|
||||
"id": "54065139",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"limit = 10^7\n",
|
||||
"totients = list(range(0, limit))"
|
||||
"def totients(limit):\n",
|
||||
" 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",
|
||||
"id": "88ef58dd",
|
||||
"id": "45a80c4b",
|
||||
"metadata": {},
|
||||
"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",
|
||||
"execution_count": 3,
|
||||
"id": "aa071cc4",
|
||||
"id": "60741588",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"limit = 10^7\n",
|
||||
"\n",
|
||||
"answer = None\n",
|
||||
"ratio = float('inf')\n",
|
||||
"\n",
|
||||
"for n in range(2, limit):\n",
|
||||
" if totients[n] != n:\n",
|
||||
" r = n / totients[n]\n",
|
||||
" if r < ratio and is_permutation_pair(n, totients[n]):\n",
|
||||
" ratio = r\n",
|
||||
" answer = n\n",
|
||||
"\n",
|
||||
"for (n, totient) in enumerate(totients(limit)):\n",
|
||||
" if n == 0 or n == 1 or totient == n - 1:\n",
|
||||
" continue\n",
|
||||
"\n",
|
||||
" for p in range(n, limit, n):\n",
|
||||
" totients[p] -= totients[p] // n"
|
||||
" \n",
|
||||
" r = n / totient\n",
|
||||
" if r < ratio and is_permutation_pair(n, totient):\n",
|
||||
" ratio = r\n",
|
||||
" answer = n"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue