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.
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:
Project Euler #65: Convergents of e
You are viewing a single comment's thread. Return to all comments →
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 ^__^