なにやらid:nise_nabeさんが階乗を高速に求めるプログラムを掘り出していたので、Rubyで書きなおしてみました。何をやってるのかさっぱりなので、ほとんど写経です。 class Integer def factorial return 1 if self < 2 @n, p, r = 1, 1, 1 log = self.log2n h, shift, high = 0, 0, 1 until h == self do shift += h h = self >> log log -= 1 len = high high = (h&0x01) == 1 ? h : h - 1 len = (high - len) >> 1 r = r * (p = p*bp(len)) if len > 0 end r << shift end def bp n m = n >> 1 return