Практические вопросы для рекурсии | Комплект 5

Опубликовано: 21 Января, 2022

Question 1
Predict the output of the following program. What does the following fun() do in general?  

C++

#include <iostream>
using namespace std;
int fun(int a, int b)
{
    if (b == 0)
        return 0;
    if (b % 2 == 0)
        return fun(a + a, b/2);
     
    return fun(a + a, b/2) + a;
}
 
int main()
{
    cout << fun(4, 3) ;
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10

C

#include<stdio.h>
 
int fun(int a, int b)
{
   if (b == 0)
       return 0;
   if (b % 2 == 0)
       return fun(a+a, b/2);
 
   return fun(a+a, b/2) + a;
}
 
int main()
{
  printf("%d", fun(4, 3));
  getchar();
  return 0;
}

Java

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    static int fun(int a, int b)
    {
        if (b == 0)
            return 0;
        if (b % 2 == 0)
            return fun(a + a, b/2);
          
        return fun(a + a, b/2) + a;
    }
  
    public static void main (String[] args)
    {
        System.out.println(fun(4, 3));
    }
}
// This code is contributed by SHUBHAMSINGH10

Python3

def fun(a, b):
    if (b == 0):
        return 0
    if (b % 2 == 0):
        return fun(a + a, b//2)
     
    return fun(a + a, b//2) + a
 
# Driver code
 
print(fun(4, 3))
 
# This code is contributed by SHUBHAMSINGH10

C#

using System;
 
class GFG{
 
    static int fun(int a, int b)
    {
        if (b == 0)
            return 0;
        if (b % 2 == 0)
            return fun(a + a, b/2);
         
        return fun(a + a, b/2) + a;
    }
     
    static public void Main ()
    {
        Console.Write(fun(4, 3));
    }
}
 
// This code is contributed by SHUBHAMSINGH10

Javascript

<script>
//Javascript Implementation
function fun(a, b)
{
    if (b == 0)
        return 0;
    if (b % 2 == 0)
        return fun(a + a, Math.floor(b/2));
      
    return fun(a + a, Math.floor(b/2)) + a;
}
document.write(fun(4, 3));
// This code is contributed by shubhamsingh10
</script>
Output: 
12

 

Он вычисляет a * b (умноженное на b).

Question 2 
In question 1, if we replace + with * and replace return 0 with return 1, then what does the changed function do? Following is the changed function. 

C++

#include <iostream>
using namespace std;
   
int fun(int a, int b)
{
   if (b == 0)
       return 1;
   if (b % 2 == 0)
       return fun(a*a, b/2);
   
   return fun(a*a, b/2)*a;
}
   
int main()
{
  cout << fun(4, 3) ;
  getchar();
  return 0;
}
 
//This code is contributed by shubhamsingh10

C

#include<stdio.h>
 
int fun(int a, int b)
{
   if (b == 0)
       return 1;
   if (b % 2 == 0)
       return fun(a*a, b/2);
 
   return fun(a*a, b/2)*a;
}
 
int main()
{
  printf("%d", fun(4, 3));
  getchar();
  return 0;
}

Java

import java.io.*;
  
class GFG {
    static int fun(int a, int b)
    {
        if (b == 0)
            return 1;
        if (b % 2 == 0)
            return fun(a*a, b/2);
         
        return fun(a*a, b/2)*a;
    }
     
    public static void main (String[] args)
    {
        System.out.println(fun(4, 3));
    }
}
//This code is contributed by shubhamsingh10

Python3

def fun(a, b):
    if (b == 0):
        return 1
    if (b % 2 == 0):
        return fun(a*a, b//2)
     
    return fun(a*a, b//2)*a
  
# Driver code
  
print(fun(4, 3))
 
# This code is contributed by shubhamsingh10

C#

using System;
 
public class GFG{
     
    static int fun(int a, int b)
    {
        if (b == 0)
            return 1;
        if (b % 2 == 0)
            return fun(a*a, b/2);
         
        return fun(a*a, b/2)*a;
    }
     
     
    static public void Main ()
    {
        Console.WriteLine(fun(4, 3));
    }
}
// This code is contributed by shubhamsingh10

Javascript

<script>
//Javascript Implementation
function fun(a, b)
{
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        return fun(a * a, Math.floor(b/2));
      
    return fun(a * a, Math.floor(b/2)) * a;
}
document.write(fun(4, 3));
// This code is contributed by shubhamsingh10
</script>
Output: 

64