3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-22 03:49:29 +01:00

applets/qrpn: Reverted nchoosek implementation and fixed the original (#71)

This commit is contained in:
rlcamp 2022-10-01 12:59:20 -07:00 committed by GitHub
parent 8721122c1f
commit abf70a2871
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -75,16 +75,12 @@ static unsigned long long gcd(unsigned long long a, unsigned long long b) {
return a;
}
static unsigned long long nchoosek(unsigned long long n, const unsigned long long k) {
static unsigned long long nchoosek(const unsigned long long n, const unsigned long long k) {
if (k > n - k) return nchoosek(n, n - k);
unsigned long long n_choose_k = 1;
for (unsigned long long i = 1; i <= k; i++, n--) {
/* give up */
if (n_choose_k / i > ULLONG_MAX / n) return 0;
n_choose_k = n_choose_k / i * n + n_choose_k % i * n / i;
}
for (size_t kr = 1; kr <= k; kr++)
n_choose_k = (n_choose_k * (n + 1 - kr)) / kr;
return n_choose_k;
}