COMP 104 : Quiz 16

Recursion

1.	What does f(8) return? What does the function do?

        	int f(int n){
                	if(n == 1)
                        return 1;
                	return n + f(n-1);
        	}



2. 	What does f(5) return? What does the function do?

        	int f(int n){
                	if(n <= 1)
                        return 1;
                	return f(n-1) + f(n-2);
        	}



 3. 	What does f(4, 5) return? What does the function do?

        	int f(int a, int b){
               	if(b == 0)
                        return 1;
               	return a * f(a, b-1);
       	}


4. 	What does f(5) return? What does the function do?

        	int f(int a){
                	if(a <= 0)
                		return 1;
                	return (f(a-1) * a);
        	}


5. 	What does f(63, 168) return? What does the function do?

      int f(int a, int b){
           if(a==b)
                return a;
           if(a > b)
                return f(a-b, b);
           else
                return f(a, b-a);
      }


Answer 1:	36	summation
Answer 2:	8	Fibonacci
Answer 3:	1024	Exp(4, 5)
Answer 4:	120	n!
Answer 5:	21	gcd(a, b)