Runtime of an easy while loop -


i have short question runtime of while loop. have given code:

calculate(int n)   = n   while(i > 0)       = i/2   

if n power of two, how while loop executed. doing revision on did @ beginning of semester , know it's not hard don't know how answer. example if n = 1, loop executed 1 time, if n = 2, loop executed 2 times, if n = 4, loop executed 3 times , on not sure how formulate mathematically.

a mathematical formula use binary logarithm:

log2(n) + 1


Comments

Popular posts from this blog

angularjs - Redirect to a new template in angular js -

Oracle SQL. How to select specific IDs where value of columns specify criteria? -

Desktop Launcher for Python Script Starts Program in Wrong Path (Linux) -