# Cache container in Js resolves the sum of Fibonacci series

### Understanding of Fibonacci Sequence

#### Let's first look at what the Fibonacci series is.

The Fibonacci column refers to such a column as 0, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368 [1]

It is particularly pointed out that item 0 is 0 and item 1 is the first 1.

This number starts with item 3, each of which equals the sum of the first two items.

#### Traditional recursive solutions result in a large number of repetitive computations.The code is as follows:

``` //f(n) = f(n-1) + f(n - 2)

var count = 0;
function fib(n){
count ++;
if(n <= 2){
return 1;
}
return fib(n - 1) + fib(n - 2);
}

fib(5);
console.log(count);
count = 0;
fib(6);
console.log(count);
count = 0;
fib(20);
console.log(count);
count = 0;
fib(21);
console.log(count);
count = 0;

//Storing arrays with arrays improves performance but has limitations
//You can find 5 or 10, but you need 100,000 tweets.
var arr = [1, 1, 2, 3, 5, 8, 13, 21, 34];

arr[0];
arr[1];
arr[100];```

#### If the problem of Fibonacci arrays is handled by a traditional recursive method, it will result in a large number of repetitive calculations, resulting in an excessive memory usage.Next, we'll make improvements.How to use a cache array:

```//Define a cache array to store the calculated Fibonacci array.
//computational procedure
//1. Get the data you want from the cache array first, and use it directly if you get it.
//2. Calculate the incoming cache array if not obtained
var cache = [];
var count = 0;
function fib(n){
count++;
//Determine if it exists in the cache array and return it directly if it exists
if(cache[n]!=undefined){
return cache[n];
}
//Calculate if not present
if(n<=2){
//Store calculation results in an array
cache[n] = 1;
return cache[n];
}
var temp = fib(n-1)+fib(n-2);
cache[n]=temp;
return temp;
}
return fib;
console.log(fib(6));

//Further, create the cache function as a constructor.
var count =0 ;
function createFib(){
var cache = [];
function fib(n){
count ++;
//1. Get data from cache
if(cache[n] !== undefined){
//If there is a direct return in the cache
return cache[n];
}
//Calculate if not in cache
if(n <= 2){
//Save calculation results in an array
cache[n] = 1;
return 1;
}
var temp = fib(n - 1) + fib(n - 2);
//Save calculation results in an array
cache[n] = temp;
return temp;
}
return fib;
}
```

### As shown in the cache function above, you can solve the problem of multiple calculations.Here we introduce the cache container.Analog the code above.

```//Create Cache Container
function CreateCache(){
var chche{};
return function(key,value){
//If a value is passed, it means the value is set
if(value!=undefined){
//Set the value and pass it into the cache container
cache[key]=value;
return cache[key];
}
//If only the key is passed, it means getting the value.
else{
return cache[key];
}
}

}

//Applying Cache Containers in Fibonacci
var count =0;
function CreateFib(){
//Create Cache Container
var fibCache =  CreateCache();
//Computation method function fib
function fib(n){
count++;
//Determine if there is a cache container
if(fibCache(n)!=undefined){
//If it exists, go back to use it directly
return fibCache(n);
}
//Calculate if it doesn't exist
if(n<=2){
//Store calculation results in containers
fibCache(n,1);
return 1;
}
var temp = fib(n-1)+fib(n-2);
fibCache(n,temp);
return temp;
}
return fib;

}
//Test it
var fib = CreateFib();
fib(5);
console.log(count);
count = 0;
fib(6);
console.log(count);
count = 0;
fib(20);
console.log(count);
count = 0;
fib(21);
console.log(count);
count = 0;```

Posted by domineaux on Sun, 12 Jul 2020 07:33:30 -0700