Stirling numbers of the first kind: Difference between revisions

m
Simplified code
m (Phix/mpfr)
m (Simplified code)
Line 133:
 
typedef struct stirling_cache_tag {
int nmax;
int* values;
} stirling_cache;
 
boolint stirling_cache_createstirling_number1(stirling_cache* sc, int n, int k) {
if (k == 0)
int* values = calloc(n * (n + 1)/2, sizeof(int));
return n == 0 ? 1 : 0;
if (n > sc->max || k > n)
return 0;
return (n > sc->n) ? NULL : &sc->values[n*(n-1)/2 + k - 1];
 
int*bool stirling_cache_getstirling_cache_create(stirling_cache* sc, int n, int kmax) {
int* values = calloc(nmax * (nmax + 1)/2, sizeof(int));
if (values == NULL)
return false;
sc->nmax = nmax;
sc->values = values;
for (int n = 1; n <= max; ++n) {
for (int k = 1; k <= n; ++k) {
int s1 = stirling_number1(sc, n - 1, k - 1);
int s2 = stirling_number1(sc, n - 1, k);
*value = values[n*(n-1)/2 + k - 1] = s1 + s2 * (n - 1);
}
}
return true;
}
Line 149 ⟶ 164:
free(sc->values);
sc->values = NULL;
 
int* stirling_cache_get(stirling_cache* sc, int n, int k) {
return (n > sc->n) ? NULL : &sc->values[n*(n-1)/2 + k - 1];
}
 
int stirling_number1(stirling_cache* sc, int n, int k) {
if (k == 0)
return n == 0 ? 1 : 0;
if (k > n)
return 0;
int* value = stirling_cache_get(sc, n, k);
if (value == NULL) {
fprintf(stderr, "Cache size is too small\n");
exit(1);
}
if (*value == 0) {
int s1 = stirling_number1(sc, n - 1, k - 1);
int s2 = stirling_number1(sc, n - 1, k);
*value = 1 + s1 + s2 * (n - 1);
}
return *value - 1;
}
 
1,777

edits