nptel swayam 1

nptel swayam

def g(y):
    b = 0
    while y >= 3:
        (y,b) = (y/3,b+1)
    return(b)

Here is what the above code is Doing:
1. It’s defining a function called g.
2. It’s defining a variable called b, and setting it equal to 0.
3. It’s saying that while y is greater than or equal to 3, do the following:
a. Set y equal to y divided by 3.
b. Set b equal to b plus 1.
4. Return b.

So, if we call g(27), it will do the following:
1. Set b equal to 0.
2. While y is greater than or equal to 3, do the following:
a. Set y equal to y divided by 3.
b. Set b equal to b plus 1.
3. Return b.

So, the first time through the loop, y is 27, so it will set y equal to 9 and b equal to 1.
The second time through the loop, y is 9, so it will set y equal to 3 and b equal to 2.
The third time through the loop, y is 3, so it will set y equal to 1 and b equal to 3.
The fourth time through the loop, y is 1, so it will set y equal to 0 and b equal to 4.
The fifth time through the loop, y is 0, so it will set y equal to 0 and b equal to 5.
The sixth time through the loop, y is 0, so it will set y equal to 0 and b equal to 6.
The seventh time through the loop, y is 0, so it will set y equal to 0 and b equal to 7.
The eighth time through the loop, y is 0, so it will set y equal to 0 and b equal to 8.
The ninth time through the loop, y is 0, so it will set y equal to 0 and b equal to 9.
The tenth time through the loop, y is 0, so it will set y equal to 0 and b equal to 10.
The eleventh time through the loop, y is 0, so it will set y equal to 0 and b equal to 11.
The twelfth time through the loop, y is 0, so it will set y equal to 0 and b equal to 12.
The thirteenth time through the loop, y is 0, so it will set y equal to 0 and b equal to 13.
The fourteenth time through the loop, y is 0, so it will set y equal to 0 and b equal to 14.
The fifteenth time through the loop, y is 0, so it will set y equal to 0 and b equal to 15.
The sixteenth time through the loop, y is 0, so it will set y equal to 0 and b equal to 16.
The seventeenth time through the loop, y is 0, so it will set y equal to 0 and b equal to 17.
The eighteenth time through the loop, y is 0, so it will set y equal to 0 and b equal to 18.
The nineteenth time through the loop, y is 0, so it will set y equal to 0 and b equal to 19.
The twentieth time through the loop, y is 0, so it will set y equal to 0 and b equal to 20.
The twenty-first time through the loop, y is 0, so it will set y equal to 0 and b equal to 21.
The twenty-second time through the loop, y is 0, so it will set y equal to 0 and b equal to 22.
The twenty-third time through the loop, y is 0, so it will set y equal to 0 and b equal to 23.
The twenty-fourth time through the loop, y is 0, so it will set y equal to 0 and b equal to 24.
The twenty-

Similar Posts