in Programming in C retagged by
544 views
2 votes
2 votes

How many times is foo activated (called), including the first "$\text{foo}(3,12)$"

$\text{max()}$ and $\text{min()}$ are functions that return maximum and minimum respectively.
 

int foo(int a, int b) {
    if (a==b) {
        return b;
    }
    int mn =min(a,b), mx =max(a,b);
        return foo(mn,mn) + foo( mx - mn , mn);
}
in Programming in C retagged by
544 views

1 comment

Total $7$ function calls is there and output of $foo(3,12)=12$
0
0

3 Answers

5 votes
5 votes

there are total 7 function call

2 votes
2 votes
The recurrence relation to count number of function call looks like,
$F(a,b)=1+F(min(a,b),min(a,b))+F(max(a,b)-min(a,b),min(a,b))$   if $a\neq b$
            =1              if $a= b$

$F(3,12)=F(3,3)+F(9,3)+1$

$F(9,3)=F(3,3)+F(6,3)+1$

$F(6,3)=F(3,3)+F(3,3)+1$

now ,$F(3,3)=1$ as a=b

putting the value in $F(6,3)$ we get ,

$F(6,3)=3$

$F(9,3)=5$

$F(3,12)=7$

so correct answer is 7.
edited by
2 votes
2 votes

Answer:

Related questions