зеркало из https://github.com/github/ruby.git
array.c: refine binomial_coefficient
* array.c (binomial_coefficient): get rid of bignums by division after each multiplications. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59692 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
96223329d0
Коммит
f0ae63b072
15
array.c
15
array.c
|
@ -5096,16 +5096,23 @@ descending_factorial(long from, long how_many)
|
|||
static VALUE
|
||||
binomial_coefficient(long comb, long size)
|
||||
{
|
||||
VALUE r, v;
|
||||
VALUE r;
|
||||
long i;
|
||||
if (comb > size-comb) {
|
||||
comb = size-comb;
|
||||
}
|
||||
if (comb < 0) {
|
||||
return LONG2FIX(0);
|
||||
}
|
||||
r = descending_factorial(size, comb);
|
||||
v = descending_factorial(comb, comb);
|
||||
return rb_int_idiv(r, v);
|
||||
else if (comb == 0) {
|
||||
return LONG2FIX(1);
|
||||
}
|
||||
r = LONG2FIX(size);
|
||||
for (i = 1; i < comb; ++i) {
|
||||
r = rb_int_mul(r, LONG2FIX(size - i));
|
||||
r = rb_int_idiv(r, LONG2FIX(i + 1));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
|
Загрузка…
Ссылка в новой задаче