The while loop for calculating a power of a number less than another number?

Discussion in 'Ruby' started by Erik the Red, Jul 29, 2005.

  1. Erik the Red

    Erik the Red Guest

    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?
    Erik the Red, Jul 29, 2005
    #1
    1. Advertising

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


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

    number /=3D 2

    puts "%i^2 < %i" % [number, max]
    bschroed@black:~/svn/projekte/ruby-things$ ruby power2.rb=20
    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



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

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
    Brian Schröder, Jul 29, 2005
    #2
    1. Advertising

  3. Erik the Red

    Kevin Guest

    this is odd since you don't need to loop to find the answer

    maxnum = 10_000

    n = 2 ** (Math.log(maxnum) / Math.log(2)).floor

    puts n + " is the largest power of 2 less than " + maxnum
    Kevin, Jul 29, 2005
    #3
  4. On 29/07/05, Kevin <> wrote:
    > this is odd since you don't need to loop to find the answer
    >=20
    > maxnum =3D 10_000
    >=20
    > n =3D 2 ** (Math.log(maxnum) / Math.log(2)).floor
    >=20
    > puts n + " is the largest power of 2 less than " + maxnum
    >=20
    >=20
    >=20


    I'd think it was a educative example?


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

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
    Brian Schröder, Jul 29, 2005
    #4
  5. Erik the Red

    Chris Pine Guest

    Well, if you are interested in another tutorial which is more
    up-to-date, try mine out:

    http://pine.fm/LearnToProgram

    (He used some of my examples in his tutorial, but not that one!)

    Chris
    Chris Pine, Jul 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,216
    Tim Slattery
    Mar 30, 2005
  2. falcon
    Replies:
    10
    Views:
    18,561
    Roedy Green
    Feb 24, 2006
  3. Graeter than or less than

    , Jun 21, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    852
    bruce barker
    Jun 21, 2007
  4. Dwight Army of Champions
    Replies:
    4
    Views:
    2,703
    John H.
    Mar 17, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    352
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page