• edited by
1,409 views
1 1 vote

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$.

3 Answers

1 1 vote

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 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:
Position:
Show:

Related questions

0 0 votes
2 2 answers
996
996 views
Tesla! asked Feb 4, 2018
996 views
Consider the following functions f() and g(). f(){ g(){ w=5; z=w+1; w=2*z+2; z=3*z-w; } print(z); }We start with $w$ and $z$ set to $0$ and execute $f()$ and $g()$ in par...
3 3 votes
2 2 answers
3.3k
3.3k views
Tesla! asked Feb 4, 2018
3,338 views
We have constructed a polynomial time reduction from problem $A$ to problem $B$. Which of the following is a valid inference?If the best algorithm for $B$ takes exponenti...
1 1 vote
2 2 answers
1.2k
1.2k views
Tesla! asked Feb 4, 2018
1,234 views
Suppose we constructed the binary search tree shown below by starting with an empty tree and inserting one element at a time from an input sequence, without any rotations...
7 7 votes
2 answers 2 answers
2.9k
2.9k views
Tesla! asked Feb 4, 2018
2,938 views
A $\text{stable sort}$ preserves the order of values that are equal with respect to the comparison function. We have a list of three-dimensional points$[(7, 1, 8),(3, 5, ...