PyPE solution to PE problem 57
PyPE solution to PE problem 57
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
===
It is possible to show that the square root of two can be expressed as an
infinite continued fraction.
2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...
By expanding this for the first four iterations, we get:
1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...
The next three expansions are 99/70, 239/169, and 577/408, but the eighth
expansion, 1393/985, is the first example where the number of digits in the
numerator exceeds the number of digits in the denominator.
In the first one-thousand expansions, how many fractions contain a numerator
with more digits than denominator?
Solution
--------
Let us first define some value for the kth expansion called phi_k, where
sqrt(2)_k ~= 1 + 1 / phi_k
and where
phi_k = n_k / d_k.
From the expansion, we can see that
phi_k+1 = 2 + 1 / phi_k,
or,
n_k+1 = 2*n_k + d_k, d_k+1 = n_k.
Using these terms, our numerator and denominator in the overall kth
expansion will be
sqrt(2)_k ~= (n_k + d_k) / n_k.
We simply iterate over the desired number of expansions and check the length
of the numerator vs the length of the denominator.
Answer
------
153