Question #92389

What does h(3231) return for the following function definition?

def h(x):
(m,a) = (1,0)
while m <= x:
(m,a) = (m*2,a+1)
return(a)

Expert's answer

Sure. I did mention x2nx\ne2^n for positive innnteger nn. So, more extended:

h(x)={log2xif x2n(log2x)+1if x=2nh(x)=\begin{cases} \lfloor\log_2 x\rfloor &\text{if } x\ne2^n \\ (log_2 x)+1 &\text{if } x=2^n \end{cases}

for any arbitrary positive integer nn.

It can be seen inserting print(m) in the while loop.

So the answer is 12,


LATEST TUTORIALS
APPROVED BY CLIENTS