The Fibonacci series of Reading Notes in the construction and interpretation of computer programs

Source: Internet
Author: User

As an example of tree recursion, the Fibonacci series points out the disadvantages: although it is a classic example of tree recursion, It is very bad to use recursion to find the Fibonacci number. This is because excessive redundant computing is generated. (See page 25th of the translated book)

Li Yang of the toplanguage group said: in the coding horror section handed down in the code book, the author stressed that it is not necessary to use recursion to calculate the factorial or Fibonacci series, because this usage is slow, the memory usage is unpredictable and the readability is poor.

In the toplanguage group, I only dive. In actual situations, I tested ironscheme interpreter, JavaScript console, and C #2005 compiler. The test process is to test the Fibonacci number, and the test value is getting bigger and bigger.

Result: The javascript console was the first to exit. The ironscheme interpreter is slower than the C #2005 compiler. This is no way-the compilation is faster than the explanation, rather than the "optimize compilation" of tail recursion.

The following is the Fibonacci series program written in javascript:

VaR fib = function (n) {If (n = 0) return 0; If (n = 1) return 1; else {return arguments. callee (n-1) + arguments. callee (n-2) ;}}var fibobj = fib (7); console. log (fibobj );

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.