Friday, March 21, 2014

Recursion-Fibonacci number generation with caching to prevent exponential time computation

1:  package com.mridul.java.algorithms;  
2:  import java.util.Arrays;  
3:  import com.mridul.java.helpers.Console;  
4:  /*  
5:   * Program to calculate Fibonacci  
6:   * numbers using Recursion.  
7:   * This Recursion implements a cache to   
8:   * increase the speed  
9:   */  
10:  public class Recursion {  
11:       //constants  
12:       private static final int CACHE_SIZE=100;  
13:       private static final int MAX_SIZE=92;  
14:       //static variables  
15:       private static Recursion rec;  
16:       private static long[] cache;  
17:       private static String error_message;  
18:       /**  
19:        * Default Constructor  
20:        */  
21:       public Recursion(){  
22:            //create cache and add first two numbers  
23:            cache=new long[CACHE_SIZE];  
24:            //initializing array to zero  
25:            Arrays.fill(cache,0);  
26:            cache[0]=0;  
27:            cache[1]=1;  
28:            error_message=String.format(  
29:                      "Please enter a valid positive integer in the range 0-%d !",  
30:                      MAX_SIZE);  
31:       }  
32:       /**  
33:        * main  
34:        * @param args  
35:        */  
36:       public static void main(String[] args) {  
37:            try{  
38:                 int size=0;  
39:                 rec= new Recursion();  
40:                 //infinite loop  
41:                 do{                      
42:                      System.out.println(  
43:                                "Enter the size of fibonacci numbers: ");  
44:                      size=Integer.parseInt(  
45:                                Console.readFromConsole());  
46:                      if(validate(size)){       
47:                           System.out.println(  
48:                                     "The fibonacci sum is : ");  
49:                           System.out.println(rec.computeFibonacci(size));  
50:                      }else{  
51:                           System.out.println(error_message);  
52:                      }  
53:                 }while(size<0);                      
54:            }catch(NumberFormatException ne){  
55:                 System.out.println(error_message);  
56:            }  
57:            catch(Throwable t){  
58:                 t.printStackTrace();  
59:            }  
60:       }  
61:       /*  
62:        * method to validate data  
63:        */  
64:       private static boolean validate(int size) {  
65:            if(size>=0 && size<=MAX_SIZE){  
66:                 return true;  
67:            }  
68:            return false;  
69:       }  
70:       /*  
71:        * method which calculates the fibonacci sequence  
72:        *   
73:        */  
74:       private long computeFibonacci(int size) {  
75:            if(size==0)return 0;  
76:            if(size==1)return 1;  
77:            if(cache[size]!=0) return cache[size];  
78:            cache[size]=computeFibonacci(size-1)+computeFibonacci(size-2);  
79:            return cache[size];  
80:       }  
81:  //End of Recursion  
82:  }  

No comments:

Post a Comment