What does g(31415927) return, for the following function definition?
def g(x):
(q,d) = (1,0)
while q <= x:
(q,d) = (q*10,d+1)
return(d)
Answers
Answered by
0
Given:
g(31415927)
( q, d ) = ( 1, 0 )
Condition:
q <= x
Then,
( q , d ) = ( q * 10, d + 1 )
Solution:
The loop returns a value if q > 31415927
When d = 8,
The value of q = 100000000 and so q > 31415927
Hence,
The function returns 8.
Answered by
0
Answer is 25
The while loop will run 25 times
As 31415927 / 2**24 =1.8... which is > 1
So for last time it will go
For 2**25 , d will be incremented 24+1=25
Then it becomes less than 1
Condition becomes false... while loop terminates
-->the no. of times x is divided by 2 & remains >1
The d will be incremented by 1.
Similar questions