From 471f74b2a12c108fb34cda6ea451d6c27a33600c Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Sun, 13 Oct 2024 16:01:33 -0500 Subject: [PATCH] Solve p26 in python --- README.rst | 2 +- docs/index.rst | 2 ++ docs/src/python/p0026.rst | 14 ++++++++++++++ python/README.rst | 1 + python/src/p0026.py | 38 ++++++++++++++++++++++++++++++++++++++ python/test_euler.py | 3 +-- 6 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 docs/src/python/p0026.rst create mode 100644 python/src/p0026.py diff --git a/README.rst b/README.rst index 292d6139..ad5ae092 100644 --- a/README.rst +++ b/README.rst @@ -107,7 +107,7 @@ Olivia's Project Euler Solutions | | | | |Lu-Cov| |br| | | | | | |LuaCheck| | +------------+----------------------------+--------+-------------------+ - | Python | CPython 3.6+ |br| | 80 | |Python| |br| | + | Python | CPython 3.6+ |br| | 81 | |Python| |br| | | | Pypy 3.6+ |br| | | |Py-Cov| |br| | | | GraalPy 23.1+ |br| | | |CodeQL| |br| | | | Browser [#]_ | | |PythonLint| | diff --git a/docs/index.rst b/docs/index.rst index 1dcbcf83..a98332ba 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -128,6 +128,8 @@ Problems Solved +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`025`|:c-d:`0025`| | | | | | |:py-d:`0025`|:rs-d:`0025`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ + |:prob:`026`| | | | | | | |:py-d:`0026`| | + +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`027`| | | | | |:js-d:`0027`| |:py-d:`0027`|:rs-s:`0027`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`028`| | | |:fr-d:`0028`| |:js-d:`0028`|:lu-d:`0028`|:py-d:`0028`|:rs-d:`0028`| diff --git a/docs/src/python/p0026.rst b/docs/src/python/p0026.rst new file mode 100644 index 00000000..0a636b97 --- /dev/null +++ b/docs/src/python/p0026.rst @@ -0,0 +1,14 @@ +Python Implementation of Problem 26 +=================================== + +View source code :source:`python/src/p0026.py` + +.. automodule:: python.src.p0026 + :members: + :undoc-members: + +.. literalinclude:: ../../../python/src/p0026.py + :language: python + :linenos: + +.. tags:: fraction, recurring-decimal, decimal-representation diff --git a/python/README.rst b/python/README.rst index 2295366b..f24e52c4 100644 --- a/python/README.rst +++ b/python/README.rst @@ -152,6 +152,7 @@ Problems Solved - ☒ `23 <./src/p0023.py>`__ - ☒ `24 <./src/p0024.py>`__ - ☒ `25 <./src/p0025.py>`__ +- ☒ `26 <./src/p0026.py>`__ - ☒ `27 <./src/p0027.py>`__ - ☒ `28 <./src/p0028.py>`__ - ☒ `29 <./src/p0029.py>`__ diff --git a/python/src/p0026.py b/python/src/p0026.py new file mode 100644 index 00000000..5374a8aa --- /dev/null +++ b/python/src/p0026.py @@ -0,0 +1,38 @@ +""" +Project Euler Problem 26 + +Problem: + +A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: + +.. code-block:: + + 1/2 = 0.5 + 1/3 = 0.(3) + 1/4 = 0.25 + 1/5 = 0.2 + 1/6 = 0.1(6) + 1/7 = 0.(142857) + 1/8 = 0.125 + 1/9 = 0.(1) + 1/10 = 0.1 + +Where 0.1(6) means 0.166666\cdots, and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. +Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. + +""" + + +def main() -> int: + remainders = {d: [1] for d in range(2, 1000)} + cycle_lengths = {} + while len(remainders): + for d in tuple(remainders.keys()): + base = 10 * remainders[d][-1] + rem = base % d + if rem in remainders[d]: + cycle_lengths[d] = len(remainders[d]) - remainders[d].index(rem) + del remainders[d] + else: + remainders[d].append(rem) + return max(cycle_lengths.items(), key=lambda p: p[1])[0] diff --git a/python/test_euler.py b/python/test_euler.py index 8cee0636..0aa434a6 100644 --- a/python/test_euler.py +++ b/python/test_euler.py @@ -19,8 +19,7 @@ answers: Dict[int, Union[int, str]] = { x: get_answer(x) for x in ( - *range(1, 26), - *range(27, 51), + *range(1, 51), 52, 53, *range(55, 61),