mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-22 20:09:43 +01:00
applets/qrpn: Reverted nchoosek implementation and fixed the original (#71)
This commit is contained in:
parent
8721122c1f
commit
abf70a2871
@ -75,16 +75,12 @@ static unsigned long long gcd(unsigned long long a, unsigned long long b) {
|
|||||||
return a;
|
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);
|
if (k > n - k) return nchoosek(n, n - k);
|
||||||
|
|
||||||
unsigned long long n_choose_k = 1;
|
unsigned long long n_choose_k = 1;
|
||||||
for (unsigned long long i = 1; i <= k; i++, n--) {
|
for (size_t kr = 1; kr <= k; kr++)
|
||||||
/* give up */
|
n_choose_k = (n_choose_k * (n + 1 - kr)) / kr;
|
||||||
if (n_choose_k / i > ULLONG_MAX / n) return 0;
|
|
||||||
|
|
||||||
n_choose_k = n_choose_k / i * n + n_choose_k % i * n / i;
|
|
||||||
}
|
|
||||||
|
|
||||||
return n_choose_k;
|
return n_choose_k;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user