PyPE solution to PE problem 26
PyPE solution to PE problem 26
Blueprint information
- Status:
- Complete
- Approver:
- None
- Priority:
- Undefined
- Drafter:
- None
- Direction:
- Needs approval
- Assignee:
- None
- Definition:
- Approved
- Series goal:
- None
- Implementation:
-
Implemented
- Milestone target:
- None
- Started by
- Scott Armitage
- Completed by
- Scott Armitage
Related branches
Related bugs
Sprints
Whiteboard
ProjectEule
===
A unit fraction contains 1 in the numerator. The decimal representation of
the unit fractions with denominators 2 to 10 are given:
^(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..., 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.
Solution
--------
The aide of Sam May was employed:
(http://
For each value of d, there are three possible cases:
1) d is coprime with 10
For any value of d that is coprime with 10, that is, they share no
common positive factor other than 1, the period of the recurring
decimal is given by the multiplicative order k. This is the smallest
value of k satisfying 10**k % d = 1.
2) d is a power of 2, 5, or 10
If d is a power of 2, 5, or 10, the decimal number is terminating.
We pre-compute a list of powers of 2, 5, and 10 less than or equal
to the limit value -- any d that is in this list will have a period
of 0.
3) d is a multiple of 2 or 5
If d is a multiple of 2 or 5, the decimal will be made up of a non-
This can be expressed as 10**(s+k) % d = 10**s. Since we only care
about the recurring part k, we can simply divide out all of the 2s
and 5s, like d = 2**a * 5**b * d0, where d0 is some representative
k for d0.
Answer
------
983