Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

Sunday, 9 December 2018

Explain different ways to achieve recursion in JavaScript

Recursion is a technique, where the function call itself directly (or) indirectly.

There are three ways to refere a function itself.
a.   the function's name
b.   arguments.callee
c.   an in-scope variable that refers to the function

Using the functions name
HelloWorld.js
function factorial(n){
    if(n <= 1){
        return 1;
    }
    
    return n * factorial(n-1);
}

console.log(`Factorial of 5 is ${factorial(5)}`);

Output
Factorial of 5 is 120

Using arguments.callee
HelloWorld.js
function factorial(n){
    if(n <= 1){
        return 1;
    }
    
    return n * arguments.callee(n-1);
}

console.log(`Factorial of 5 is ${factorial(5)}`);


Using an in-scope variable that refers to the function
HelloWorld.js

var fact = function factorial(n){
    if(n <= 1){
        return 1;
    }
    
    return n * fact(n-1);
}

console.log(`Factorial of 5 is ${factorial(5)}`);



Previous                                                 Next                                                 Home

Wednesday, 12 February 2014

How to increase stack size in java

Consider the Below Program

class StackOverFlow{
 static int count = 0;
 static void fun(){
  count++;
  if(count == 100000){
   System.out.println("Count is " + count);
   System.exit(0);
  }
  fun();
 }

 public static void main(String args[]){
  fun();
 }
}

Above program has a function named fun(), Which will call itself recursively for 100000 times. When I tried to run the above program, I got below error.

Exception in thread "main" java.lang.StackOverflowError
at StackOverFlow.fun(StackOverFlow.java:5)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)
at StackOverFlow.fun(StackOverFlow.java:8)

StackOverflowError thrown because an application recurses too deeply.

How to Increase Stack space
Java provides a command line option “-Xss” to increase the thread stack size.

Syntax
-Xss<size>[g|G|m|M|k|K]

Following command runs application with 10MB stack size.

java -Xss10m StackOverFlow

Output
Count is 100000

Remember, Memory is limited, So instead of depending on increasing stack size, focus on your program, find out where it is consuming more stack memory.

The root cause for StackOverflowError is probably recursion

Stack Memory                                                 Heap Memory                                                 Home

Stack Memory

Stack is a special region in computer memory, that stores temporary variables like local variables, parameters, function calls etc.,

Stack works in last in first out (LIFO) order. Whenever a new local variable initialized inside a function, it is pushed onto the stack, when the function finished execution, all the local variables pushed by that function onto the stack are removed from the stack.

CPU organizes the stack memory for function calls, local variables creation, recursion.

CPU organizes stack memory so efficiently, so reading from and writing to stack variables is very fast.

There is a limit on the size of variables that can be stored on the stack

The default stack size depends on the platform

Example Below are some default stack sizes, These may change from version to version and platform to platform

Platform Default
Windows IA32 64 KB
Linux IA32 128 KB
Linux x86_64 256 KB
Windows IA64 320 KB
Windows x86_64 128 KB
Linux IA64 1024 KB (1 MB)
Solaris Sparc 512 KB

Example Of Recursion
class Stack{
 static void print(int n){
  if(n==0) return;
  else{
   print(--n);
   System.out.println(n);
  }
 }

 public static void main(String args[]){
  print(4);
 }
}
   
Output
0
1
2
3


Some Points to Remember
1. In java, you can never put an object on stack, only object reference can be stored on stack. Objects always stored in heap
 

Lambda Expression that computes a value                                                 Increase Stack Size                                                 Home