Euler discovered and proved that ∑n–2 = π2/6. This is a slowly converging series and the discovery probably required several digits. The naïve summation omits the tail of the series after n terms. That tail sums to about 1/n. Here is how to get a very good estimate of the sum of the tail and thus reduce the error to about 1/(last term)3.
#include <stdio.h>
static double sq(double x){return x*x;}
int main(){int i=0; double s = 0;
double pi = 3.141592653589793238;
while(i < 100) {++i; s += sq(1./i);}
s += 1/(i+0.5);
{double p2 = sq(pi), p2o6 = p2/6;
printf("%19.16f %19.16f %19.16f %e\n",
p2, p2o6, s, p2o6-s);}
return 0;}
This program yields:
#include <stdio.h>
#include <math.h>
typedef double f;
static f sq(f x){return x*x;}
static f F(f x){return log(x)/sq(x);}
static f intg(f x){return -log(x)/x - 1/x;}
static f isn(f x){return intg(x + 0.5) - intg(x - 0.5);}
static f err(f x){return F(x) - isn(x);}
int main(){f x = 1;
while (x < 2000000) {printf("%e %e\n", x, err(x)); x *= 10.;}
return 0;}
#include <stdio.h>
#include <math.h>
typedef double f;
static f sq(f x){return x*x;}
static f F(f x){return log(x)/sq(x);}
static f intg(f x){return -log(x)/x - 1/x;}
static void mx(int k){int i=0; double s = 0;
while(i < k) {++i; s += F(i);}
s -= intg(i+ 0.5);
printf("%19.16f %d\n", s, k);}
int main(){mx(10); mx(100); mx(1000);
mx(10000); mx(100000); mx(1000000);
return 0;}
This program yields: 0.9376812399594099 10 0.9375485917322104 100 0.9375482548490655 1000 0.9375482543165695 10000 0.9375482543158479 100000 0.9375482543158530 1000000I conclude that indeed the error is about (last term)3/2 and that the answer to the original problem is: