From: hadley wickham Date: 2006-08-08T01:07:16+09:00 Subject: Re: nextPowerOf2(n) > Here's another take > > irb(main):016:0> class Fixnum > irb(main):017:1> def next_power_of_2 > irb(main):018:2> trial = 1 > irb(main):019:2> trial <<= 1 while trial < self > irb(main):020:2> return trial > irb(main):021:2> end > irb(main):022:1> end > => nil > irb(main):023:0> (-1..10).collect { | i | [i, i.next_power_of_2] } > => [[-1, 1], [0, 1], [1, 1], [2, 2], [3, 4], [4, 4], [5, 8], [6, 8], > [7, 8], [8, 8], [9, 16], [10, 16]] > irb(main):024:0> > > This should be fairly fast since at first glance it's o(log2(n)) When the alternatives are O(1), that's not that great! Hadley