Java recursion implements the Fibonacci series and java recursion.

Source: Internet
Author: User

Java recursion implements the Fibonacci series and java recursion.

The Programming Technique of program calling itself is called recursion ). Recursive Algorithms are widely used in programming languages. A process or function has a method that calls itself directly or indirectly in its definition or description, it usually converts a large and complex problem into a small problem similar to the original problem to solve it, the recursive strategy can describe the repeated computing required for the problem-solving process with only a small number of programs, greatly reducing the amount of code in the program. The ability to recursion lies in the use of limited statements to define an infinite set of objects. In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. If the boundary condition is not met, recursive advances. If the boundary condition is met, recursive returns. -- This Is What Baidu encyclopedia says.

To put it bluntly, the recursive method itself calls its own operation. The following example shows the famous Fibonacci series.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144,233,377,610,987,159, 2584, 4181,6765, 10946, 17711, 28657,46368 ......
We can see that the third number is obtained by adding the first two numbers.

If a normal loop is used to solve the problem, it is as follows:

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;} System. err. result of println (n + "number:" + numn );}}

The running result is:

Result of 10 numbers: 34

This is an operation using a normal cyclic method. If recursion is used, it is as follows:

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 this case, recursion does not need to continue calling and end recursion. When n = 1 or 2, 0 or 1 is returned, rather than calling the recursive method itself.

The two most important conditions for recursion are the conditions for self-calling and ending recursion.

Recursion is a waste of resources because it is called by itself. The running time is much longer than the loop, and the operation is slow and the efficiency is low.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.