[前][次][番号順一覧][スレッド一覧]

ruby-changes:47576

From: nobu <ko1@a...>
Date: Wed, 30 Aug 2017 17:26:21 +0900 (JST)
Subject: [ruby-changes:47576] nobu:r59692 (trunk): array.c: refine binomial_coefficient

nobu	2017-08-30 17:26:16 +0900 (Wed, 30 Aug 2017)

  New Revision: 59692

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59692

  Log:
    array.c: refine binomial_coefficient
    
    * array.c (binomial_coefficient): get rid of bignums by division
      after each multiplications.

  Modified files:
    trunk/array.c
Index: array.c
===================================================================
--- array.c	(revision 59691)
+++ array.c	(revision 59692)
@@ -5096,16 +5096,23 @@ descending_factorial(long from, long how https://github.com/ruby/ruby/blob/trunk/array.c#L5096
 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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]