java-Recursive-Fibonacci sequence __ function

Source: Internet
Author: User

The programming skill that the program calls itself is called recursion (recursion). Recursion as an algorithm is widely used in programming languages. A procedure or function has a method of calling itself directly or indirectly in its definition or description, it usually transforms a large and complex problem layer into a small scale problem similar to the original problem, and the recursive strategy can describe the multiple computations needed in the process of solving problems with only a small number of programs. Greatly reduces the amount of code in the program. The ability of recursion is to define an infinite set of objects with limited statements. In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. When the boundary condition is not satisfied, recursion advances, and when the boundary condition is satisfied, recursion returns. --This is the Baidu Encyclopedia said.

In fact, it is the recursive method itself to invoke its own operations, the following examples illustrate this example is very famous-Fibonacci series.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368 ...
You can see that the third number is the sum of the preceding two numbers.

This is true if you use a normal loop to resolve this:

public class feibo{public
    static void Main (string[] args) {
        int num1=0;
        int num2=1;
        int numn=1;
        int n=10;
        for (int i = 3; I <=n; i++) {
            numn=num1+num2;
            num1=num2;
            Num2=numn;
        }
        The result of System.err.println (n+) is: "+numn";
    }

The results of the operation are:

The results of 10 numbers are: 34

This is done using the normal loop method, if recursion is used:

    public static int recursion (int n) {

        if (n==1) {return
            0;
        }

        if (n==2) {return
            1;
        }
        return recursion (n-1) +recursion (n-2);
    }

Recursion requires an end condition, in which case recursion does not need to continue the call, ending recursion. The above case end condition is that when n=1 or 2, it returns 0 or 1, rather than continuing to invoke the recursive method itself.

The two most important conditions for recursion are to call yourself and end the condition of recursion.

Because the recursive is to call themselves so waste resources, running longer than the cycle, running slowly, the efficiency of the end.

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.