Paper discussion: mathematical induction, recursion, stack

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

Mathematical induction

Mathematical induction)It is a mathematical proof method. It is often used to prove that a proposition (a description of a phenomenon) is valid within the range of natural numbers. With the development of modern mathematics, the proof within the range of natural numbers actually forms the basis of many other fields (such as mathematical analysis). Therefore, mathematical induction is crucial to the entire mathematical system.

 

Mathematical induction is very simple. If we want to prove that a proposition is true for natural number N, then:

The first step proves that the proposition is true for n = 1.

The second step assumes that the proposition is true for N, and N is an arbitrary natural number. Under this assumption, the proposition is true for n + 1.

Proof of proposition

 

Think about the two steps above. They actually mean that the proposition is true for n = 1-> the proposition is true for n = 2-> the proposition is true for n = 3 ...... Until infinity. Therefore, the proposition is true for any natural number. This is like a domino card. We are sure that the collapse of N will lead to the collapse of N + 1, and then the first bone card will be pushed down to ensure the collapse of any bone card.

 

Let's take a look at the mathematical induction to prove the Gaussian summation formula:

N is an arbitrary natural number.

(This formula is said to have come out of Gaussian Elementary School. The teacher punished the class and had to calculate the sum of 1 to 100 to go home. So Gauss came up with the above method. Are geniuses forced out ?)

 

Our proposition is: Gaussian summation formula is true for any natural number n.

The following describes how to prove the mathematical induction:

The first step n = 1, the left side of the equation (the sum of 1) is 1, the right side (the right side of the formula is n = 1) is also 1, the two sides of the equation are equal, the equation is true, therefore, the proposition is true for n = 1.

The second step assumes that the above formula is true for any N, that is, the sum of 1 to n isN * (n + 1)/2

For n + 1, the left side of the equation (from 1 to n + 1) is equal to N * (n + 1)/2 + (n + 1), that is(N + 1) * (N + 2)/2,

N on the right of the equation is replaced by N + 1(N + 1) * (N + 2)/2,

The two sides of the equation are equal, and the equation is true. Therefore, when the hypothesis is true for N, the proposition is true for n + 1.

Therefore, the proposition is proved.

 

Recursion

Recursion (recursion)Is an important concept in computers. It refers to a computer.ProgramCall itself. To ensure that the computer does not fall into an endless loop, recursion requires the program to have a base case that can be achieved ). For example, the following program is used to calculate the Gaussian summation formula:

  /*   * Gauss summation   */  int   F (n) {  If  (n =  1  ) {  return   1 ; ///   base case  }  else  {  return  F (n- 1 ) + N; ///   induction  }} 

The value of F (1) and the relationship between F (N) and F (n-1) are defined in the program. This is the embodiment of mathematical induction. To get F (N), you must calculate F (n-1); To F (n-1), you must calculate F (n-2 )...... Until F (1 ). Because we already know the value of F (1), we can fill in all the previous vacancies and finally return the value of F (n.

Recursion is the Program Implementation of mathematical induction in computers. When using Recursive Design programs, we set the base case and assume that we will get n-1 results and implement n results. This is like mathematical induction. We only focus on the initial and cohesion, rather than the specific steps.

 

Stack

Recursion is implemented using the stack data structure. As we mentioned above, the calculation of F (n) requires F (n-1); the calculation of F (n-1) requires F (n-2 )....... Before we find F (1), there will be many vacancies: F (n-1) value what? What is the value of F (n-2? ...... What is the value of F (2? What is the value of F (1? Our first question is what F (n) is. As a result, this question leads to the next question ...... The answer to each question depends on the next question until we find the first question that can be answered: what is the value of F (1?

We use stacks to save our questions during the exploration process. In C language, the function is called to use the stack to record the departure situation and return address. Recursion is a function call to itself, so it is natural that recursion uses stacks to save our "questions ".

Let's assume that the stack grows down. First, when we call f (100 ),

 
ReturnF (n-1) + N;

F (100) pause the execution and record the current status, such as the value of N. Then call f (99) and add a frame to the stack until the call f (98)... stack continues to grow until F (1 ). F (1) returns result 1 to F (2 ). F (1) stack frame deletion, transfer to F (2) frame context to continue execution

 
ReturnF (n-1) + N;

Then return to F (3)... Until F (99) returns to F (100), and execute

ReturnF (n-1) + N;

Returns the value of F (100.

 

The above process is automatically completed by the C compiler. Implement RecursionAlgorithmYou can also manually implement the stack. In this way, the running efficiency can be improved.

 

Summary

Mathematical induction

Recursion

Stack

 

Welcome to the "paper discussion: algorithms and data structures" series.

 

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.