On 29/07/05, Erik the Red <the.g.man.x / gmail.com> wrote:
> http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_02/while.html
> 
> The code is as follows:
> 
> number = 1
> while number < 10_000
>   number *= 2
> end
> 
> number /= 2
> 
> puts number.to_s + " is the highest " +\ "power of 2 less than 10,000"
> 
> I don't quite understand this, especially since:
> 
> a. His program output is clearly not correct. 16 384 is greater than 10
> 000.
> 
> b. His comments don't make sense.
> 
> What does this code do exactly?

bschroed@black:~/svn/projekte/ruby-things$ cat power2.rb 
max = 10_000
number = 1
while number < max
 number *= 2
end

number /= 2

puts "%i^2 < %i" % [number, max]
bschroed@black:~/svn/projekte/ruby-things$ ruby power2.rb 
8192^2 < 10000

Are you sure you used the right code? Seems like you forgot the
division by 2 at the end.

Try to print ot the value of number after each step, maybe that will
help your understanding.

Regards,

Brian



-- 
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/