Stirling numbers of the second kind: Difference between revisions
m
Simplified code
m (Phix/mpfr) |
m (Simplified code) |
||
Line 117:
typedef struct stirling_cache_tag {
int
int* values;
} stirling_cache;
if (k == n)▼
int* values = calloc(n * (n + 1)/2, sizeof(int));▼
return 1;▼
if (k == 0 || k > n || n > sc->max)▼
return 0;▼
}▼
if (values == NULL)
return false;
sc->
sc->values = values;
for (int n = 1; n <= max; ++n) {
int s1 = stirling_number2(sc, n - 1, k - 1);▼
int s2 = stirling_number2(sc, n - 1, k);▼
values[n*(n-1)/2 + k - 1] = s1 + s2 * k;
}▼
}▼
return true;
}
Line 133 ⟶ 148:
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];
▲ if (k == n)
▲ return 1;
▲ if (k == 0 || k > n)
▲ return 0;
▲ }
▲ int s1 = stirling_number2(sc, n - 1, k - 1);
▲ int s2 = stirling_number2(sc, n - 1, k);
▲ *value = 1 + s1 + s2 * k;
▲ }
}
|