Monday, December 17, 2018

RECURSIVE

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
void recursion() {
   recursion(); /* function calls itself */
}

int main() {
   recursion();
}
The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Recursive is a function call inside a certain function calling itself
Recursive Function, suitable for recursive problem
Example :
 Factorial (n) or n! defined as follows :
 n! = 1, for n = 0;
 n! = n * (n-1)!, for n > 0
 4! = 4 * 3!
 3! = 3 * 2!
 2! = 2 * 1!
 1! =  1* 0!
 0! =  1
 Trace back : 4! = 1*2*3*4 = 24



Recursive Function has two components:
Base case:
  return value(constant) without calling next recursive call.
Reduction step:
  sequence of input value converging to the base case.
Example: (Factorial function)
Base case : n = 0
Reduction step: f(n) = n * f(n-1)


Example: (Iterative vs Recursive)

1.


Number Factorial

       The following example calculates the factorial of a given number using a recursive function −
#include <stdio.h>

unsigned long long int factorial(unsigned int i) {

   if(i <= 1) {
      return 1;
   }
   return i * factorial(i - 1);
}

int  main() {
   int i = 12;
   printf("Factorial of %d is %d\n", i, factorial(i));
   return 0;
}
When the above code is compiled and executed, it produces the following result −
Factorial of 12 is 479001600

2.


Fibonacci Series

The following example generates the Fibonacci series for a given number using a recursive function −
#include <stdio.h>

int fibonacci(int i) {

   if(i == 0) {
      return 0;
   }
 
   if(i == 1) {
      return 1;
   }
   return fibonacci(i-1) + fibonacci(i-2);
}

int  main() {

   int i;
 
   for (i = 0; i < 10; i++) {
      printf("%d\t\n", fibonacci(i));
   }
 
   return 0;
}
When the above code is compiled and executed, it produces the following result −
0 
1 
1 
2 
3 
5 
8 
13 
21 
34



Recursive Drawback
Although recursive code more concise it needs:
More memory consumption – as stack memory is needed
Takes longer time, should traverse through all recursive call using stack
Recursive Best Practice
Generally, use recursive solution if:
Difficult to solve iteratively.
Efficiency using recursive has been reached
Efficiency is less important in comparison with readability
Memory efficiency and execution time are not the main concern
Consider carefully speed and efficiency using iterative approach, rather than nice logical design using recursive



Fibonacci Number
sequence: 0, 1, 1, 2, 3, 5, 8, 13 ...
Relation between the number define recursively as follows:
Fib(N) = N           if N = 0 or 1
Fib(N) = Fib(N-2) + Fib(N-1)   if N >= 2



int Fib(int n) {
   int f;
   if(n==0) f = 0;
      else if(n==1) f = 1;
         else f = Fib(n-2) + Fib(n-1);
   return f;
}


No comments:

Post a Comment