1)What would happen if we call gcd(m,n) with m positive and n negative in the following definition?

def gcd(m,n):
  if m < n: 
    (m,n) = (n,m)
  if (m % n) == 0:
    return(n)
  else:
    diff = m-n
    return (gcd(max(n,diff),min(n,diff)))
Answer : (a) 
Reason : When m = -n , m%n will return 0, hence the loop will stop. But in all other cases, the loop will not terminate because difference will be always positive, and hence, m%n will never be 0.

 

2)What can we say about a number n if h(n) returns True for the function h given below?

def h(n):
  for i in range(2,n):
    if n%i == 0:
      return(True)
  return(False)

 

 
 
 
 
Answer : (b) n is a composite number.
Reason : Simple prime number logic. Returns True if number is not prime(i.e. Composite) and false if number if Prime.

 

3)What does f(120,13) return for the following function defintion?

def f(m,n):
  ans = 1
  while (m - n >= 0):
    (ans,m) = (ans*2,m-n)
  return(ans)

 

Answer : 512.
Reason : In above program, while loop terminates when the value of m became less then the value of n. So, by understanding this loop if we calculate initially m/n = 120/13 = 9.  Effectively what will be calculated is 2 raised to the answer of m^n if we see only the math path(ans=ans*2 for 9 times). By calculation, it will be 512( 2 ^ 9).

 

4)What does g(9000,3) return for the following function definition?

def g(x,y):
  val = 0
  while (x > y):
    (val,x) = (val+1,x/y)
  return(val)

 

Answer : 8
Reason : Basically, the program is simple and calculates how many times loop will execute. So if the value of ( x/y <= y ) then loop will terminate. So by calculation 9000/3^8 it gives answer more then 3 but when we will calculate 9000/3^9(answer less than y which is 3) then loop will terminate. So the final answer is 8. Simple Maths Equation :- (x/y^n), where n is val.

 

Categories: NPTEL solution

3 Comments

Brijesh · July 30, 2017 at 10:30 AM

Nice one. Keep going!

Abhishek Shah · August 1, 2017 at 5:32 AM

Nice Work Brother! keep it up! keep helping us ☺

    Mit Patel · August 4, 2017 at 4:34 PM

    Hello Abhishek,
    Thanks for your best wishes. We hope that our work will be helpful to you.
    From,
    Hackademic Team

Leave a Reply

Your email address will not be published. Required fields are marked *