The slow computations used 999,999,999 terms. I seem to recall from my first numerical analysis (Scientific Computing I) course in the Mathematics Department at the Georgia Institute of Technology with Professor Gunter Meyer in the Summer of 1982 that computing a truncated infinite series is more accurate to start with the smallest terms.
// RiemannZetaFunctionWin32Console.c (c) Saturday, July 13-15, 2024
// James Pate Williams, Jr. some computations use 999,999,998 terms
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef signed long long sll;
// https://en.wikipedia.org/wiki/Particular_values_of_the_Riemann_zeta_function#cite_note-7
long double EvenZeta(int n)
{
sll A[12] = { 0ll,
6ll, 90ll, 945ll, 9450ll, 93555ll, 638512875ll,
18243225ll, 325641566250ll, 38979295480125ll,
1531329465290625ll, 13447856940643125ll };
sll B[12] = { 0ll, 1ll, 1ll, 1ll, 1ll, 1ll, 691ll, 2ll,
3617ll, 43867ll, 174611ll, 155366ll };
long double pi = 4.0 * atan(1.0);
return (long double)B[n / 2] * powl(pi, n) / (long double)A[n / 2];
}
long double Zeta(long s, long *terms)
{
int n = 0;
long double sum = 0.0;
*terms = 0;
for (n = 1000000000; n >= 2; n--)
{
sum += 1.0 / powl(n, s);
(*terms)++;
}
return sum + 1.0;
}
void PrintEvenZetaValue(int n)
{
clock_t time0 = clock();
long double ez = EvenZeta(n);
clock_t time1 = clock();
double runtime = ((double)time1 - time0) / CLOCKS_PER_SEC;
printf_s("Runtime in seconds to compute Zeta(%ld) = %16.15Lf: %Lf\n",
n, ez, runtime);
}
void PrintZetaValue(int n)
{
clock_t time0 = clock();
long terms = 0;
long double zeta = Zeta(n, &terms);
clock_t time1 = clock();
double runtime = ((double)time1 - time0) / CLOCKS_PER_SEC;
printf_s("Runtime in seconds to compute Zeta(%ld) = %16.15Lf terms = %ld: time = %Lf\n",
n, zeta, terms, runtime);
}
int main()
{
PrintEvenZetaValue(2);
PrintEvenZetaValue(4);
PrintEvenZetaValue(6);
PrintEvenZetaValue(8);
PrintZetaValue(2);
PrintZetaValue(4);
PrintZetaValue(6);
PrintZetaValue(8);
PrintZetaValue(3);
PrintZetaValue(5);
PrintZetaValue(7);
PrintZetaValue(9);
return 0;
}