Number Fibonacci algorithm thinking (1) Do not calculate the Fibonacci recursive column!

When ever learn recursion, I believe the vast majority of people have used Fibonacci recursion column to learn it.

When I learn recursion is a teacher also deliberately let's think about how to optimize their performance, so we added some variables, parameters used to pass data to reduce memory consumption, or make recursive partitioning, is divided into several sub-recursive one by one and finally merge after calculation over.

but! I had been confined to a recursive learning to live, due to the recursive learning, so always thinking about is how to achieve recursive, but ignoring the problem should be addressed with the highest easiest way until today in an article comments area to see practical recursive Fibonacci columns to test the computing performance is suddenly thought: why use the Fibonacci recursion, obviously is an array ah! In terms of the number of columns on a single array of home and more closer to its structure it ...

As mentioned in the second edition of "Programming Pearls": structure determines the algorithm, to such structures already have an existing data structure of data to meet the priority should obviously be in line with the calculation method using the existing data structure is more efficient, if you're learning or you are like me, have limited algorithm -> data, you may wish to try it in a different way from this moment: data determines the form of algorithms to be programmed. Of course, it is recommended according to the actual scene or target priority principle, after a specific scene or target to achieve a multi-faceted attempt to try to find the optimal solution for the ultimate goal (the premise of your boss to give you optimize the time, if the boss does not give time, then then the next record, right? forgot lost back better than okay)

Fibonacci array of practical realization of the following methods:

Fibonacci = const (n-) => {
   IF ( typeof ! n-== 'Number' n-|| <= 0 ) {
     the throw  new new Error ( `$ {} n-MUST BE A 0 Number Greater Within last `); 
  } 
  IF (n- <. 3 ) {
     return . 1 ; 
  } the else  IF (n> 1476) {   // maximum value when n is equal to 1476, more than 1476 have been calculated value exceeds the value of the size may represent js, only get all Infinity 
    return Infinity ; 
  } 
  const resArr = new new the Array (n-) .fill (. 1);   // omit initialization of the first and second terms 
  for (the let = I 2; I <n-; I ++ ) { 
    resArr [I]= resArr[i - 1] + resArr[i - 2];
  }
  return resArr[n - 1];
}

console.log(fibonacci(1476));  // 1.3069892237633987e+308
console.log(fibonacci(1477));  // Infinity

If you need to use a lot of words, can speak here of resArr into the closure or global (not recommended), then improved the method so that if n is greater than before the push into the resArr on the basis of the original, if it is smaller than before return to direct, but I believe we have less than a few times now, but still can put other ideas algorithm, frequently used in calculation of time-consuming data caching algorithm is recommended to use the form to reduce the average time-consuming.

I did not carry out a special algorithm to learn, so you can record it encounters this line of thinking, welcome positive criticism!

 

Guess you like

Origin www.cnblogs.com/YMaster/p/11356860.html