Skip to main content

Nested Recursion

 • A recursive function will pass parameter as a recursive call is called nested recursion. 

• A recursive function taking recursive call as its parameter is called nested recursion.

Pseudocode-:

void fun(int n)
  if( condition){
    _______
    _______
  fun(fun(n-1))
}

Example-:

class Test{
public static int fun(int n){
   if(n>100)
      return n-10;
  else
      return fun(fun(n+11));
}
public static void main(String[] args){
  int r=fun(95);
  System.out.print(r);
}
}


                Output is-: 91

In nested recursion, one of the arguments to the recursive function is the recursive function itself! These functions tend to grow extremely fast. A good example is the classic mathematical function, "Ackerman's function. It grows very quickly (even for small values of x and y, Ackerman n(x,y) is extremely large) and it cannot be computed with only definite iteration (a completely defined for() loop for example); it requires indefinite iteration (recursion, for example).

Ackerman's function 

int ackerman(int m, int n) {
 if (m == 0) 
return(n+1); 
else if (n == 0) 
return(ackerman(m-1,1)); 
else 
return(ackerman(m-1,ackerman(m,n-1))); 
}

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...