[Reprint] Java deduction of recursive functions

Source: Internet
Author: User

Java deduction of recursive functions

Lxgljj


 

1. Definition of recursive functions:

A: A recursive function is a self-called function. It calls itself directly or indirectly in the function body, that is, the nested function is the function itself.

2. Recursive Method: There are two types of recursive Methods: direct and indirect.

A: Direct recursion: the function itself is called in the function.

Example 1: The following code calculates the nth item of the Fibonacci series. The first and second items of the Fibonacci series are 1, and each of the following items is the sum of the first two items, 1, 1, 2, 3, 5, 8, 13 ....

Program code:

Public class test {

Public static void main (string ARGs []) {

Int X1 = 1;

Int sum = 0;

Int n = 7;

 

For (INT I = 1; I <= N; I ++ ){

X1 = func (I );

Sum = sum + x1;

}

System. Out. println ("sum =" + sum );

}

 

Public static int func (int x ){

If (x> 2)

Return (func (X-1) + func (X-2 ));

Else

Return 1;

}

}

 

B: Indirect recursion: other functions are called in the function, and other functions call this function.

Example 2: Calculate the above Fibonacci series using indirect recursion.

Program code:

Public class test {

Public static void main (string ARGs []) {

Int X1 = 1;

Int sum = 0;

Int n = 7;

 

For (INT I = 1; I <= N; I ++ ){

X1 = func1 (I );

Sum = sum + x1;

}

System. Out. println ("sum =" + sum );

}

Public static int func1 (int ){

Int B;

B = func2 ();

Return B;

}

 

Public static int func2 (int B ){

If (B> 2)

Return (func1 (B-1) + func1 (B-2 ));

Else

Return 1;

}

}

3. Why do we need recursive functions? What are the disadvantages of recursive functions?

A: recursion aims to simplify program design and make the program readable.

Example 3: Do not use recursive functions to calculate the above Fibonacci sequence.

Program code:

Public class test {

Public static void main (string ARGs []) {

Int n = 7;

Int A = 1, B = 1, temp;

Int sum = 2;

 

For (INT I = 3; I <= N; I ++ ){

Temp = a + B; A = B; B = temp;

Sum = sum + temp;

}

System. Out. println ("sum =" + sum );

}

}

From the example above, we can find that non-recursive functions are more efficient but difficult to program and have poor readability. The disadvantage of recursive functions is that it increases the system overhead. That is to say, every recursion consumes more memory on the stack.

 

4. Recursive conditions:

A: A statement to complete the task must meet the recursive requirements (reduce rather than divergence ).

5. Recursive advanced:

Example 4:

Programming: How many cows have been generated every year since the fourth year of birth if a heifer has been born?

 

Program code:

Public class test3 {

Public static void main (string ARGs []) {

Int n = 10; // The number of years to view

System. Out. println ("Total" + cattle (n) + "head calf! ");

}

Public static int cattle (int n ){

If (n <= 0)

Return 0;

If (n <= 3)

Return 1;

Return cattle (n-1) + cattle (n-3); // here the recursion should be well understood.

}

}

 

Rule: recursive functions for this type of problem are:

If the requirement is for the fourth year from birth, the recursive function is cattle (n-1) + cattle (n-3 ),

If the requirement is for the fifth year from birth, the recursive function is cattle (n-1) + cattle (n-4 ),

....

And so on.

(All the original code is successfully debugged under jbuilderx)

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.