applets/qrpn: Fixed nchoosek implementation (#70)

This commit is contained in:
rlcamp 2022-10-01 09:46:58 -07:00 committed by GitHub
parent 2cf09ca415
commit 9d420fd5c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 5 deletions

View File

@ -13,6 +13,7 @@
#include <time.h>
#include <float.h>
#include <complex.h>
#include <limits.h>
/* begin simple math functions we want to expose via the interpreter */
@ -74,12 +75,16 @@ static unsigned long long gcd(unsigned long long a, unsigned long long b) {
return a;
}
static unsigned long long nchoosek(const unsigned long long n, const unsigned long long k) {
if (1 == k) return n;
static unsigned long long nchoosek(unsigned long long n, const unsigned long long k) {
if (k > n - k) return nchoosek(n, n - k);
unsigned long long n_choose_k = n * (n - 1) / 2;
for (size_t kr = 3; kr <= k; kr++)
n_choose_k *= (n + 1 - kr) / kr;
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;
}
return n_choose_k;
}