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

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -