Daniel Martin wrote: > > That's not the way anyone does exponentiation with integers. Instead, > you represent the exponent as a sum of powers of 2 (which you already > have as its binary representation) and then do something like, e.g., > this to find n ** 10: > > n2 = n * n > n4 = n2 * n2 > n8 = n4 * n4 > n ** 10 = n8 * n2 > > Still, n ** 2 does result in a multiplication, but I think that what > we're seeing is that n * n involves two lookups of the value of the > variable "n", whereas n ** 2 involves only one retrieval of n. That > extra lookup operation swamps any of the actual mathematical > operations. This also explains why with a large literal simple > multiplication is faster than doing ** 2. > BUT: a simple n**2 can fail, whereas a n*n cannot. Look at the thread "Bignum limits ?" to see the reason (and it won't work only in ruby 1.8.5 I think). Just to play with this: a=2**262144;0 b=a**20;0 c=b**2;0 d=b*b;0 And just for the hell of it: d