-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b679b8
commit be71b23
Showing
6 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters