Programming, Data Structures And Algorithms Using Python Week 1 Nptel Assignment Answer
1. What does f(27182818) return, for the following function definition?
```python
def f(x):
d=0
while x > 1:
(x,d) = (x/2,d+1)
return(d)
```
- The function `f(x)` returns the number of times `x` can be divided by 2 until it becomes less than or equal to 1. For `f(27182818)`, the value is 24.
2. What is h(60)-h(45), given the definition of h below?
```python
def h(n):
s = 0
for i in range(2,n):
if n%i == 0:
s = s+i
return(s)
```
- Calculate `h(60)` and `h(45)` and then subtract them.
- For `h(60)`, the factors are 2, 3, 4, 5, 6, 10, 12, 15, 20, 30; sum = 112.
- For `h(45)`, the factors are 3, 5, 9, 15; sum = 32.
- Therefore, `h(60) - h(45) = 112 - 32 = 80`.
3. For what value of n would `g(375, n)` return 4?
```python
def g(m, n):
res = 0
while m >= n:
(res, m) = (res + 1, m/n)
return(res)
```
- Solve `g(375, n) = 4` to find the value of `n`.
- Starting with `m = 375`, `res = 0`, the loop will execute until `m` is less than `n`.
- `g(375, 3) = 4` because 375 divided by 3 four times gives a result less than 3. Therefore, the value of `n` is 3.
4. Consider the following function `mys`:
```python
def mys(m):
if m == 1:
return(1)
else:
return(m * mys(m-1))
```
- The correct option is: "The function terminates for positive n with `mys(n) = factorial of n`".