Skip to content

Commit

Permalink
Solve p26 in python
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 16, 2024
1 parent 0b679b8 commit 471f74b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`|
Expand Down
14 changes: 14 additions & 0 deletions docs/src/python/p0026.rst
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions python/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
Expand Down
38 changes: 38 additions & 0 deletions python/src/p0026.py
Original file line number Diff line number Diff line change
@@ -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]
3 changes: 1 addition & 2 deletions python/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 471f74b

Please sign in to comment.