edited by
707 views
1 votes
1 votes

What does the following function compute in terms of $n$ and $d$, for integer values of $d$ ? Note that the operation $/$ denotes floating point division, even if the arguments are both integers.

function foo(n,d){
if (d == 0){
return 1;
}else{
if (d < 0){
return foo(n,d+1)/n;
}else{
return n*foo(n,d-1);
}}}
  1. $log_dn$ if $d < 0$ , $n^d\ $if$ \ d>0$.
  2. $n^d$ for all the values of $d$.
  3. $n \times d$ if $d>0$ ,$n\div d$ if $d<0$.
  4. $n \times d$ for all the values of $d$.
edited by

3 Answers

1 votes
1 votes

Let's take two cases:

Case 1: $n=5, d=3$

$foo(5,3)$

$5*foo(5,2)$

$5*5*foo(5,1)$

$5*5*5*foo(5,0)$

$5*5*5*1= 5^3 = n^d$

Case 2: $n=5, d=-1$

$foo(5,-1)$

$foo(5,0)/5$

$\frac{1}{5}= 5^{-1}= n^d$

In both the case option B) satisfies

0 votes
0 votes

IF you observer the logic then you will find that when ever the valve of d is greater than zero the valve n is multiplied to itself and d is decremented, and this will be done till the value of d!=0.

Similarly if d is negative then valve of n is divided by itself and d is incremented by 1 and this continue till d!=0.

So, Ans C

reshown by
Answer:

Related questions