We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #65: Convergents of e
Project Euler #65: Convergents of e
Sort by
recency
|
13 Discussions
|
Please Login in order to post a comment
For a full explanation consider Project Euler 65
Python 3 solution, using the fact that the continued fraction of e has a pattern of 2 1 1 4 1 1 6 11, ..., from index 2:
python solution
Java BigInteger
Observed patterns how the fraction changes from the required convergent to back to 3rd convergent.
I handle the 1st and 2nd convergents explicitly. I represent the fraction using two variables num and den which stand for numerator and denominator.
First, I find out which multiple of 2 in the series is the closest to the required convergent. for example, if the required convergent is 10, then I make the fraction ready for calculation from '6' in the series. The variable a denotes the next multiple of 2 coming up in the series. for example, the fraction i prepared for convergent 10 was built using '6', but the next multiple of 2 in the series will be 4, so I assign a as '4'. I also made two functions, one: to "add an upcoming number" (whether 'a' or 1) to the fraction and two: to find the "reciprocal" of the fraction and then dividing the reciprocal by 1. (swapping num and den) The rest is pretty much self explanatory
This was my code when I thought long would be enough:
But obviously that did not work out as the numbers go way crazy! Then i converted the above same logic into BigInteger. Here it is:
Drop any queries in the comments, thanks ^__^
easiest one, read out https://en.wikipedia.org/wiki/Continued_fraction Hope you will find an interseting sequence :)