138 lines
4.0 KiB
Plaintext
138 lines
4.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ec776294",
|
|
"metadata": {},
|
|
"source": [
|
|
"# [Totient Permutation](https://projecteuler.net/problem=70)\n",
|
|
"\n",
|
|
"SageMath's implementation of $\\phi(n)$ is fast enough that you could brute force this if you wanted, but if we're clever, we can solve more quickly.\n",
|
|
"\n",
|
|
"We'll write a simple function for determining if two numbers are digit permutations of each other."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "e6757165",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def is_permutation_pair(a, b):\n",
|
|
" s, t = str(a), str(b)\n",
|
|
" return sorted(s) == sorted(t)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bdeb8c77",
|
|
"metadata": {},
|
|
"source": [
|
|
"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$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "524fca40",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"limit = 10^7\n",
|
|
"totients = list(range(0, limit))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "88ef58dd",
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "aa071cc4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"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",
|
|
" continue\n",
|
|
"\n",
|
|
" for p in range(n, limit, n):\n",
|
|
" totients[p] -= totients[p] // n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "d4819aa5",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"8319823"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"answer"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "40cf1e01",
|
|
"metadata": {},
|
|
"source": [
|
|
"Note: lots of people in the problem thread make the assumption that the answer must be a [semiprime](https://en.wikipedia.org/wiki/Semiprime). However, Steendor points out that for certain upper bounds, this assumption does not hold. For instance, $2817 = 3^2 \\times 313$ and $\\phi(2817) = 1872$, and 2817/1872 is the lowest ratio until 2991. 2817 may be an exception rather than the norm (all the other numbers in A102018 up to $10^8$ are semiprimes); nevertheless, this solution avoids making the assumption.\n",
|
|
"\n",
|
|
"## Relevant sequences\n",
|
|
"* All numbers $n$ such that $\\phi(n)$ is a digit permutation: [A115921](https://oeis.org/A115921)\n",
|
|
"* Subsequence of A115921 such that $n/\\phi(n)$ is a record low: [A102018](https://oeis.org/A102018)"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|