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
Post a Comment