summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-08-30 07:55:19 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-08-30 07:55:19 (GMT)
commit96223329d0b11a76e37a91b08a828571b5e97172 (patch)
treeb0e5d4805b60da14f76085813305516bc4629ca1 /array.c
parent6d5a4fefd07175df8264b2b6834687773709a207 (diff)
array.c: refine descending_factorial
* array.c (descending_factorial): reduce factorial multipication. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/array.c b/array.c
index 674e354..e131d1f 100644
--- a/array.c
+++ b/array.c
@@ -5079,10 +5079,16 @@ permute0(const long n, const long r, long *const p, char *const used, const VALU
static VALUE
descending_factorial(long from, long how_many)
{
- VALUE cnt = LONG2FIX(how_many >= 0);
- while (how_many-- > 0) {
- VALUE v = LONG2FIX(from--);
- cnt = rb_int_mul(cnt, v);
+ VALUE cnt;
+ if (how_many > 0) {
+ cnt = LONG2FIX(from);
+ while (--how_many > 0) {
+ long v = --from;
+ cnt = rb_int_mul(cnt, LONG2FIX(v));
+ }
+ }
+ else {
+ cnt = LONG2FIX(how_many == 0);
}
return cnt;
}