Rahul
2011-Aug-14 04:56 UTC
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 (m**2) < 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Aug-14 07:18 UTC
Re: What Is Wrong With Code - I am beginner- Time Limit Exceed Problem
On 14 August 2011 05:56, Rahul <raikrahul-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > if (m**2) < 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 xI 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Winters
2011-Aug-14 07:20 UTC
Re: What Is Wrong With Code - I am beginner- Time Limit Exceed Problem
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)/2puts "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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.