-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathem-accel.c
More file actions
160 lines (141 loc) · 6.14 KB
/
em-accel.c
File metadata and controls
160 lines (141 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* em-accel — Euler-Maclaurin Convergence Acceleration Tool
*
* Standalone command-line tool for accelerating slowly-converging sums.
* Implements Euler-Maclaurin tail correction + Richardson extrapolation.
*
* Usage:
* em-accel zeta2 [N] Accelerate ζ(2) = π²/6 (Basel problem)
* em-accel zeta3 [N] Accelerate ζ(3) = Apéry's constant
* em-accel harmonic [N] Estimate H_N with tail correction
* em-accel custom S [N] Accelerate ζ(S) for integer S
*
* Example:
* $ em-accel zeta2 49145
* Raw partial sum: 1.644913719105317 (5 correct digits)
* EM corrected (o=1): 1.644934064532421 (10 correct digits)
* EM corrected (o=3): 1.644934066848226 (16 correct digits)
* Richardson: 1.644934066848226 (16 correct digits)
* Exact π²/6: 1.644934066848226
*
* Formally verified in Lean 4 (afld_proof v5.21.0).
*
* Copyright (c) 2026 Christian Kilpatrick. MIT License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static double partial_sum(int s, int N) {
double sum = 0.0;
for (int k = 1; k <= N; k++)
sum += 1.0 / pow((double)k, (double)s);
return sum;
}
static double em_tail(int s, double N, int order) {
if (s < 1 || N <= 0) return 0.0;
double term = 0.0;
if (s == 1) {
term = log(N) + 0.5772156649015329;
return term;
}
term = 1.0 / ((s - 1) * pow(N, s - 1));
if (order >= 1) term += 0.5 / pow(N, s);
if (order >= 2) term -= (double)s / (12.0 * pow(N, s + 1));
if (order >= 3) term += (double)(s * (s + 1) * (s + 2)) / (720.0 * pow(N, s + 3));
return term;
}
static double richardson(double s_n, double s_2n, int p) {
double factor = pow(2.0, p);
return (factor * s_2n - s_n) / (factor - 1.0);
}
static int count_correct_digits(double computed, double exact) {
if (exact == 0.0) return 0;
double rel_err = fabs(computed - exact) / fabs(exact);
if (rel_err <= 0.0) return 16;
int digits = (int)(-log10(rel_err));
if (digits < 0) digits = 0;
if (digits > 16) digits = 16;
return digits;
}
static void run_zeta(int s, int N, const char *name, double exact) {
printf("╔══════════════════════════════════════════════════════════════╗\n");
printf("║ Euler-Maclaurin Convergence Acceleration ║\n");
printf("║ Series: %-48s ║\n", name);
printf("╠══════════════════════════════════════════════════════════════╣\n");
clock_t start = clock();
double raw = partial_sum(s, N);
printf(" N = %d terms\n\n", N);
printf(" Raw partial sum: %.16f (%d digits)\n",
raw, count_correct_digits(raw, exact));
for (int ord = 1; ord <= 3; ord++) {
double corrected = raw + em_tail(s, (double)N, ord);
printf(" EM corrected (o=%d): %.16f (%d digits)\n",
ord, corrected, count_correct_digits(corrected, exact));
}
double raw_half = partial_sum(s, N / 2);
double em_half = raw_half + em_tail(s, (double)(N / 2), 3);
double em_full = raw + em_tail(s, (double)N, 3);
double rich = richardson(em_half, em_full, s);
printf(" Richardson extrap: %.16f (%d digits)\n",
rich, count_correct_digits(rich, exact));
printf("\n Exact value: %.16f\n", exact);
clock_t end = clock();
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
int raw_digits = count_correct_digits(raw, exact);
int best_digits = count_correct_digits(rich, exact);
printf("\n Improvement: %d → %d correct digits (%.0f× better)\n",
raw_digits, best_digits,
best_digits > raw_digits ? pow(10.0, best_digits - raw_digits) : 1.0);
printf(" Time: %.3f ms\n", elapsed * 1000.0);
printf("╚══════════════════════════════════════════════════════════════╝\n");
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <zeta2|zeta3|zeta4|harmonic|custom S> [N]\n", argv[0]);
fprintf(stderr, "\nExamples:\n");
fprintf(stderr, " %s zeta2 ζ(2) = π²/6, N=49145\n", argv[0]);
fprintf(stderr, " %s zeta3 100000 ζ(3) = Apéry's constant\n", argv[0]);
fprintf(stderr, " %s custom 5 10000 ζ(5) with 10000 terms\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "zeta2") == 0) {
int N = (argc > 2) ? atoi(argv[2]) : 49145;
run_zeta(2, N, "ζ(2) = π²/6 (Basel Problem)", M_PI * M_PI / 6.0);
} else if (strcmp(argv[1], "zeta3") == 0) {
int N = (argc > 2) ? atoi(argv[2]) : 100000;
run_zeta(3, N, "ζ(3) = Apéry's constant", 1.2020569031595943);
} else if (strcmp(argv[1], "zeta4") == 0) {
int N = (argc > 2) ? atoi(argv[2]) : 50000;
run_zeta(4, N, "ζ(4) = π⁴/90", pow(M_PI, 4) / 90.0);
} else if (strcmp(argv[1], "harmonic") == 0) {
int N = (argc > 2) ? atoi(argv[2]) : 100000;
double exact = log((double)N) + 0.5772156649015329 + 0.5 / N;
run_zeta(1, N, "Harmonic series H_N", exact);
} else if (strcmp(argv[1], "custom") == 0) {
if (argc < 3) {
fprintf(stderr, "Usage: %s custom S [N]\n", argv[0]);
return 1;
}
int s = atoi(argv[2]);
int N = (argc > 3) ? atoi(argv[3]) : 50000;
char name[64];
snprintf(name, sizeof(name), "ζ(%d)", s);
double exact = 0.0;
if (s == 2) exact = M_PI * M_PI / 6.0;
else if (s == 4) exact = pow(M_PI, 4) / 90.0;
else if (s == 6) exact = pow(M_PI, 6) / 945.0;
else {
exact = partial_sum(s, 10000000) + em_tail(s, 10000000.0, 3);
}
run_zeta(s, N, name, exact);
} else {
fprintf(stderr, "Unknown series: %s\n", argv[1]);
return 1;
}
return 0;
}