Construction and interpretation of computer programs> 1. Construction Process abstraction> 1.2. Process and the computation they produce

Source: Internet
Author: User

Linear recursion:

Function Factorial (n ){
Return N = 1? 1: N * factorial (n-1 );
}

 

Linear iteration:

Function Factorial (n ){
Return Fact_iter (1, 1, n );
}
Function Fact_iter (product, counter, max_count ){
Return Counter> max_count? Product:
Fact_iter (counter * product), (counter + 1), max_count );
}

The shape of a linear recursive computing process is first expanded and then shrunk. The interpreter also saves the linear recursive calculation steps.

The linear iteration saves the computing results without the need to explain the Save calculation steps.

Therefore, linear iteration prevails in terms of speed. However, in terms of understanding, linear recursion prevails.

 

Tree recursion:

Function FIB (n ){
Return N = 0? 0: N = 1? 1:
FIB (n-1) + fib (n-2 );
}

 

estimate the time and space of the algorithm .

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.