So I need a mathematical expression for the following loop, but I can’t seem to grasp it. I am assuming I am just missing something simple.
while a <= b
a = a + a
end
Using an analysis, what would be the run time of this function?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The run time is dependent on the logarithm of
b. In other words, the time complexity isO(log N).You can see this if you start
aat 1 andbat 256. Each time through the loop,ais doubled so that there are only nine iterations (would be eight if the condition was< b).Each doubling of the
bvalue will result in one extra iteration.Of course, this is complexity analysis, the runtime depends on a host of other factors such as (almost certainly not an exhaustive list):
a:a == 0gives infinite run time,a == b + 1gives you constant run time.