{ "cells": [ { "cell_type": "markdown", "id": "b7b7784e", "metadata": {}, "source": [ "# [Reciprocal Cycles](https://projecteuler.net/problem=26)\n", "\n", "Another easy problem with SageMath." ] }, { "cell_type": "code", "execution_count": 1, "id": "20a3987a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "983" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(range(2, 1000), key=lambda d: (1/d).period())" ] }, { "cell_type": "markdown", "id": "6dc68724", "metadata": {}, "source": [ "But let's talk about how we would write our own algorithm for calculating the period.\n", "\n", "If $d = 2^a 5^b n$ where $n$ is coprime to 2 and 5, then the [period](https://mathworld.wolfram.com/DecimalPeriod.html) of $\\frac{1}{d}$ is the [multiplicative order](https://en.wikipedia.org/wiki/Multiplicative_order) of 10 modulo $n$. This is the same as finding the smallest positive $k$ such that\n", "$$10^k \\equiv 1 \\pmod{n}$$\n", "(Wondering why this is called multiplicative order? It has to do with the mathematical concept of [groups](https://w.wiki/NeN), but you don't need to be familiar with them to apply the formula.)\n", "\n", "Based on the definition, we can easily write a function that computes multiplicative order that is efficient enough for this problem, but it's not very efficient in general. The computation is a special case of the [discrete logarithm](https://en.wikipedia.org/wiki/Discrete_logarithm), which has no known efficient algorithm in general." ] }, { "cell_type": "code", "execution_count": 2, "id": "d967be38", "metadata": {}, "outputs": [], "source": [ "def multiplicative_order(a, n):\n", " if n == 1:\n", " return 1\n", " \n", " g = 1\n", " for k in range(1, n):\n", " g *= a\n", " g %= n\n", " if g == 1:\n", " return k" ] }, { "cell_type": "code", "execution_count": 3, "id": "dc51eb0e", "metadata": {}, "outputs": [], "source": [ "def period(d):\n", " while d % 2 == 0:\n", " d //= 2\n", " while d % 5 == 0:\n", " d //= 5\n", " \n", " return multiplicative_order(10, d)" ] }, { "cell_type": "markdown", "id": "14f53b42", "metadata": {}, "source": [ "Personally, I don't feel it's immediately obvious *why* multiplicative order can be used to calculate these periods. Here's a deeper dive into why this works, if you're curious.\n", "\n", "## Explanation\n", "As above, let $d = 2^a 5^b n$, where $n$ is coprime to 2 and 5, and consider the unit fraction $u=\\frac{1}{d}$. The decimal representation of this fraction (as with any fraction) may have a [fractional part](https://en.wikipedia.org/wiki/Fractional_part) with $q$ leading digits, followed by a repetend of $r$ digits.\n", "\n", "Let's make note of a trick you probably already know: multiplying a number by 10 gives the same result as if you just moved the original number's decimal point to the right one digit (e.g. $1.25 \\times 10 = 12.5$). This means that $10^q u$ has a decimal representation with only the repetend in its fractional part. If you were to then multiply it by $10^r$, the integer part would increase, but the fractional part would stay the same since it repeats every $r$ digits.\n", "\n", "The above is important for understanding the following: $10^q 10^r u - 10^q u$ *is an integer*. How do we know? Because by the logic above, the fractional part of both $10^q 10^r u$ and $10^q u$ only have the repetend, so they cancel out when we subtract, leaving us with an integer. It immediately follows that $d(10^q 10^r u - 10^q u) = 10^q 10^r - 10^q$ is also an integer that has $d$ as a factor. We can express this as\n", "$$10^{q+r} \\equiv 10^q \\pmod{d}$$\n", "Through the properties of [modular arithmetic](https://en.wikipedia.org/wiki/Modular_arithmetic), we can cancel common factors of 2 and 5 in $10^{q+r}$, $10^q$, and $d$ until we end up with\n", "$$10^r \\equiv 1 \\pmod{n}$$\n", "By definition, $r$ is the multiplicative order of 10 modulo $n$.\n", "\n", "As a concrete example of the above, consider $d = 2^4 \\times 5 \\times 63 = 5040$. The decimal representation of $u = \\frac{1}{d}$ is $0.0001(984126)$, where 984126 is the repetend. Therefore $q=4$ and $r=6$. Sure enough, $10^q 10^r u - 10^q u = 1984125$ is an integer; therefore, $10^{10} \\equiv 10^4 \\pmod{5040}$, and $10^6 \\equiv 1 \\pmod{63}$.\n", "\n", "## Relevant sequences\n", "* Periods of reciprocals: [A007732](https://oeis.org/A007732)\n", "\n", "#### Copyright (C) 2025 filifa\n", "\n", "This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)." ] } ], "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 }