JS Implementation of factorial and Recursion

Source: Internet
Author: User

JS Implementation of factorial and Recursion


At first glance, it is indeed very easy to implement factorial by recursion, but it still requires a little skill to implement recursion well.

Recursion means that the program continuously calls its own programming skills during execution. Of course, there must also be a clear termination condition. Otherwise, it won't be able to stop ~ Here we can simply implement recursive factorial.

 
 
  1. function factorial( n ){
  2. return ( n <= 1 ) ? 1 : n * factorial( n-1 );
  3. }

For factorial recursion, because it only calls itself once each time, it will not cause some major problems, but for some relatively complex recursion, it is necessary to do some technical caching. For example, the most typical use case of recursion is to calculate the Fibonacci series, but each call will call its own function twice here. So, 2 to 4, 4 to 8, and the magnitude increases exponentially, when calculating a slightly larger number, some running time may occur, or may cause stack overflow.

 
 
  1. function fibonacci( n ){
  2. return ( n <= 2 ) ? 1 : fibonacci( n-1 ) + fibonacci( n-2 );
  3. }

  4. function t(n){
  5. console.time("a");
  6. console.log( fibonacci( n ));
  7. console.timeEnd("a");
  8. }


The above is the specific data. It seems that there is a significant consumption point. After all, it takes 2 s to reach 42. After all, the tested host is considered as a superior, to optimize the caching method, try the following:

 
 
  1. var fibArr = [ undefined, 1, 1 ];
  2. function fibonacci( n ){
  3. var nFib = fibArr[ n ];
  4. return nFib
  5. ? nFib
  6. : ( fibArr[ n ] = fibonacci( n-1 ) + fibonacci( n-2 ) );
  7. }


Well, there is not much explanation. This is the result of optimization. Think about it and we can feel that we have reduced the computing magnitude of the Fibonacci number from the secondary index of 2 to the secondary index of the constant n, and because of the cache, the magnitude of operation will be reduced in multiple times, then I was shocked by the running speed. This method also relieves stack overflow.

If you remember correctly, there is a theory that all recursive loops can be converted into iterative loops.

 
 
  1. function fibonacci( n ){
  2. var x = 1, y = 1, i=2, t;
  3. while( i < n ){
  4. t = y;
  5. y = x + y;
  6. x = t;
  7. i++;
  8. }
  9. return y;
  10. }



It seems that this is a little more awesome. In fact, its magnitude is the same as that of the recursive formula with cache after optimization, but his amount is only calculated, and every amount of recursion calls a method, this is also the biggest difference between the two.

Well, let's just give a brief look at our own views. We need to think carefully about the recursive method. If it is difficult to play a game, we will be paralyzed. If you are not professional, please forgive me. If you have any mistakes, please correct me. Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.