Reciprocal Palindromes
November 30, 2021
Over on YouTube, Michael Penn proves that the sum of reciprocals of the palindromes converges:
But he doesn’t compute the value to which the sum converges.
Your task is to compute the infinite sum. When you are finished, you are welcome to read a suggested solution, or to post your own solution or discuss the exercise in the comments below.
You are adding up the reciprocals in reverse order of size, so you will converge on a sum that is unchanged when the next value is added in (seems to be about 3.370283225788489823 using doubles). We could generate the palindromes in reverse order (starting from somewhere suitably small) but a possibly nicer solution is to use the Kahan summation algorithm (https://en.wikipedia.org/wiki/Kahan_summation_algorithm). Alternatively, you can peek at https://oeis.org/A118031 to see the answer.
Some code using Kahan summation:
And some output, first column is compensated sum, second is “naive” sum, third is iterations. Convergence is slow so will have to run for a while to compare with OEIS value:
After running for nearly 24 hours and adding up the first trillion reciprocals, we’ve only got as far as the 11th decimal place, but looks like the summation algorithm is doing its job (https://oeis.org/A118031 gives 3.370283259497..):
Each decimal place takes around 10 times longer than the last, so I’ve stopped it there rather than running until Christmas. It would be fun to add something to the code that uses extrapolation to estimate where we are currently converging to.
It goes a little faster if the todouble() parameter is made a const reference.
https://pastebin.com/fpniQL4u