in Programming edited by
207 views
1 vote
1 vote

What does the following function compute for $x \neq 0?$

float isi1(float x, int y){
    if (y==0){ return 1 ;}
    else if (y>0) {return isi1(x,-y);}
    else { return isi1(x, y+1)/x;} 
}
in Programming edited by
207 views

3 Answers

3 votes
3 votes
Best answer
Renaming isi1(x, y) as $f(x,y)$ where $x$ is float and $y$ is an integer.

Definition of $f(x,y)$ is given for $x \neq 0 $ as:

\[
    f(x,y)=
\begin{cases}
    1,& \text{ } y = 0\\
    f(x,-y),              & \text{} y > 0\\
    \frac{f(x,y+1)}{x},              & \text{}y < 0\\
\end{cases}
\]

Case 1: $y > 0$

$f(x, \ y) = f(x, \ -y) =  \frac{f(x, \  -y+1)}{x} = \frac{f(x, \ -y+2)}{x^2} = … = \frac{f(x, \ -y+a)}{x^a}$ for $a \geq 1$

So, $f(x,y) = \frac{f(x ,\ a \ – \ y)}{x^a} $

Since, base case is $f(x,0) = 1,$ So, $f(x ,\ a \ – \ y) = 1$ when $a \ – \ y = 0 \Rightarrow a = y$

Hence, $f(x,y) = \frac{1}{x^y} $ for $y > 0$

 

Case 2: $y < 0$

$f(x, \ y) = \frac{f(x, \ y+1)}{x} = \frac{f(x, \ y+2)}{x^2} = … = \frac{f(x, \ y+a)}{x^a}$ for $a \geq 1$

So, $f(x,y) = \frac{f(x ,\ a \ + \ y)}{x^a} $

Since, base case is $f(x,0) = 1,$ So, $f(x ,\ a \ + \ y) = 1$ when $a \ + \ y = 0 \Rightarrow a = \ - \ y$

Hence, $f(x,y) = \frac{1}{x^ { \ – \ y}} $ for $y < 0$

From case (1) and (2), $f(x,y) = \frac{1}{x^{|y|}} = x^ {\ – \ |y|}$

Hence, given function computes $x^ {\ – \ |y|} \  ; x \neq 0$
selected by
0 votes
0 votes

The function isil(x,y) returns 1 / ( x ^ |y| ) = x^( – |y| )

Refer  the following pic where dry run of 2 isil functions are shown.


 

 

edited by

1 comment

can you prove that given function is $x^{-|y|} \ ?$
0
0
0 votes
0 votes

$x^{-\left | y \right |}$ ;$x\neq 0$

Related questions