Project Euler 461 - Almost Pi
Official link: https://projecteuler.net/problem=461
Note: Code runs in ~600 sec
Thought Process
Having done a similar problem the method of this one was quite obvious for me.
The generic name for this type of algorithm seems to be "Meet in the middle algorithm"
Just generate all possible e^(k/n) - 1
In the case n = 10,000, k goes up to 14,211 since after than e^(k/n) - 1 > π
Generate all pairs fn(a) + fn(b) where a < b and fn(a) + fn(b) < π
Go through every pair (fn(a), fn(b))
binary search in the list of pairs for the value π - fn(a) - fn(b). This gives us another pair (fn(c), fn(d))
See if π - fn(a) - fn(b) - fn(c) - fn(d) is our minimal error so far
If it is, update minimum error seen so far and correspondingly a*a + b*b + c*c + d*d
Interactive Code
Enter an Integer (yourinput)
Code will output g(yourinput)