{ "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", "Rather than calculate the totients of every single number up to $10^7$, we'll start with just the primes - for a prime $p$, $\\phi(p) = p-1$. We'll store these primes in a [min-heap](https://en.wikipedia.org/wiki/Heap_(data_structure)) ordered by $\\frac{p}{\\phi(p)}$." ] }, { "cell_type": "code", "execution_count": 2, "id": "244e05bd", "metadata": {}, "outputs": [], "source": [ "import heapq\n", "\n", "limit = 10^7\n", "\n", "primes = prime_range(limit)\n", "queue = [(p / (p - 1), (p, p - 1)) for p in primes]\n", "heapq.heapify(queue)" ] }, { "cell_type": "markdown", "id": "9a057da0", "metadata": {}, "source": [ "Then we'll search to find the value $n$ with the smallest ratio that is also a digit permutation of its totient. We'll check composite values by pushing $np$ to the queue for each prime $p$.\n", "\n", "When we find a value that is a digit permutation of its totient, we'll know that it also has the smallest ratio and can stop." ] }, { "cell_type": "code", "execution_count": 3, "id": "99c4267a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8319823" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "answer = None\n", "visited = set()\n", "while queue != []:\n", " _, (n, totient) = heapq.heappop(queue)\n", " \n", " if n in visited:\n", " continue\n", " visited.add(n)\n", " \n", " if is_permutation_pair(n, totient):\n", " answer = n\n", " break\n", " \n", " for p in primes:\n", " q = n * p\n", " if q >= limit:\n", " break\n", " \n", " if n % p != 0:\n", " new_totient = totient * (p - 1)\n", " else:\n", " new_totient = totient * p\n", " \n", " ratio = q / new_totient\n", " heapq.heappush(queue, (ratio, (q, new_totient)))\n", " \n", "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. This solution avoids making that assumption." ] } ], "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 }