Power of 2 code

hello I am new here and new to ruby (no programming experience but
willing to learn)

I am following “Daniel Carrera” tutorial I am already at while loops and
fairly I understand of it but the one thats bugging me is the exercise
of the power of 2.

heres the exercise:

Rewrite this last program so that the computer asks for the maximum
number and the program computes the corresponding power of two.

Enter maximum value:
124123
2^17 =131072 is the highest power of 2 less than 124123

heres my code;

puts "Enter a maximum value: "
number =gets.chomp.to_i
high =number
while number < 10_000
number *= 2
end
puts high.to_s + " is the highest " +
"power of 2 less than " + number.to_s

this is the site:
http://localdoc.scusa.lsu.edu/ruby/Tutorial/part_02/while.html

thanks for anyone answering

His example is wrong, as the result should be 2^16

ruby-1.9.2-p180 :008 > 217
=> 131072
ruby-1.9.2-p180 :009 > 2
16
=> 65536

If you are learning, recommend:
https://pine.fm/LearnToProgram/

On Sat, Oct 22, 2011 at 05:37, Ray DarkScythe

hey thanks for the answer john and also for the link I check it out.
I am almost finish to this tutorial after I am done with it i check your
link.

has anyone have the answer of “Daniel Carrera” exercise I cannot seem to
find
the answer sheet of the exercise so that i can compare my answer are
right or wrong. here is the link of the said exercise.

http://localdoc.scusa.lsu.edu/ruby/Tutorial/index.html

On Oct 22, 2011, at 12:37 AM, Ray DarkScythe wrote:

hello I am new here and new to ruby (no programming experience but
willing to learn)

I am following “Daniel Carrera” tutorial I am already at while loops and
fairly I understand of it but the one thats bugging me is the exercise
of the power of 2.

I would make three changes to your code.

First, in your code you have a variable named “number”.
Actually, there are a pair of numbers. I think one
reason you are having trouble is that you have confused
yourself about what “number” is supposed to be doing.

So I suggest that you use more descriptive variable
names such as (for this example) “limit” and “exponent”.

Secondly, I’d do the exponentiation explicitly rather
than implicitly the way your code does.

The third problem you are having is a classic
“off by one” error. You test ever-increasing values
until one is too big. You then print out the
excessively large value. But the desired result is last
value that is not too big. In other words, you report
a value that has been increased one too many times.
(Off-by-one errors are extremely common when looping.)

So, using more descriptive variable names, explicit
exponentiation, and reworking the while loop to
“test ahead” to avoid the “off by one” problem:

puts "Enter a maximum value: "
limit = gets.chomp.to_i
exponent = 0
while (2 ** (exponent+1)) < limit
exponent += 1
end
puts exponent.to_s + " is the highest " +
"power of 2 less than " + limit.to_s

That’s actually a tricky bit of coding. I have to admit that
I initially tripped over the off-by-one issue while giving
it a try. So don’t be surprised that it gave you some
trouble.

Have fun and keep plugging away.

PS - You may have noticed that Ruby programmers often use
very short variable names. So, for the above, some might
use “l” and “e”. This tendency is largely bravado. I
suggest you stick with longer variable names while learning.
You can save the keystrokes when you are more experienced
and want to show off your refined poetic sensibilities.

Dan N.

hi Ray,

hmmm, a couple of things -

heres my code;

puts "Enter a maximum value: "
number =gets.chomp.to_i
high =number ### why this?
while number < 10_000 ### and why this?
number *= 2 ### this multiplies, doesn’t raise exponentially
end
puts high.to_s + " is the highest " +
"power of 2 less than " + number.to_s

maybe something like this is what you’re looking for…

#######
puts “Enter maximum value:”

max = gets.chomp.to_i
puts

n = 0

while 2 ** (n + 1) < max
n += 1
end

puts “2^#{n} = #{2 ** n} is the highest power of 2 less than #{max}”
#######

here, the while loop increases the value of the exponent n by 1
unless the result is greater than max.

Enter maximum value:
124123

=> 2^16 = 65536 is the highest power of 2 less than 124123

hth,

j

Rewrite this last program so that the computer asks for the maximum
number and the program computes the corresponding power of two.

Enter maximum value:
124123
2^17 =131072 is the highest power of 2 less than 124123

Loops can be confusing sometimes.
This won’t help you with the loops because it doesn’t have loops.
But, maybe it can still be helpful.
Or, not. :slight_smile:
If the puts statement is confusing, then do it your way.
(number-1).to_s(2).size-1 is the exponent.

#########

puts "Enter a maximum value: "
number = gets.chomp.to_i

puts “2^#{(number-1).to_s(2).size-1}” if number >= 1

Harry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs