What Is Wrong With Code - I am beginner- Time Limit Exceed Problem

To change this template, choose Tools | Templates

and open the template in the editor.

puts “Hello World”
def squareroot(a)
u=a
l=0
while (l<=u)
m = l + (u-1)/2
if (m2) < a
l = m + 1
elsif (m
2) > a
u = m-1
elsif
m**2 == a
return m
else
return l-1

end

end
end
x = squareroot(55)
print x

Rahul

On 14 August 2011 05:56, Rahul [email protected] wrote:

l = m + 1
end
end
x = squareroot(55)
print x

I guess it is never coming out of your while loop. Have a look at the
Rails Guide on debugging to find out how to break into code using
ruby_debug and see what is going on. Also you could try putting some
write statements in the function to see what is happening.

Hint: It may not help but what happens if you try squareroot(55.0)?
If that helps make sure you understand why (using debug or print
statements).

Colin

On 14/08/11 05:56, Rahul wrote:

To change this template, choose Tools | Templates

and open the template in the editor.

puts “Hello World”
def squareroot(a)
u=a
l=0
while (l<=u)
m = l + (u-1)/2

    puts "u = #{u}, l = #{l}, m = #{m}"
 if (m**2) < a

[snip rest of code]

Try adding in that extra line of code and I think you might understand
your logic error.

HTH
John