C # recursion

Source: Internet
Author: User

What is a recursive function/method?


Either method can invoke other methods and call itself, and when this method calls itself, we call it a recursive function or a recursive method!

There are usually two characteristics of recursion:


1. The recursive method always calls itself until certain conditions are met, that is to say, there must be an exit;

2. The recursive method will have some parameters, and it will pass these new parameter values to itself; (self-tuning);

Recursion is typically used for: ①. Factorial ②. Fibonacci sequence;

1. Factorial

Factorial (!) is the product of all positive integers less than a certain number;

Note: 0 is neither a positive integer nor a negative integer; 0 is an integer;

0!=1

1!=1

2!=2*1!=2

3!=3*2!=6

4!=4*3!=24

5!=5*4!=120

...

n!=n* (n-1)!

Here is an implementation of the factorial calculation (no recursion is used):

public long factorial (int n) {

if (n==0)

return 1;

Long value=1;

for (int i=n;i>0;i--) {

Value*=i;

}

return value;

}

Recursive method:

Pubic long factorial (int n) {

if (n==0) {

return 1;

Return n*factorial (n-1);

}

}

You know, the factorial of n is actually the factorial of n-1 multiplied by N, and n>0;

It can be expressed as factorial (n) =factorial (n-1) *n;

This is the return value of the method, but we need a condition that is exit ( Note: Recursion must have an exit )

If the n=0 returns 1;

Now the logic of this program should be very clear, so that we can easily understand.

2. (Fibonacci) Fibonacci sequence:

The Fibonacci series are numbers arranged in the following order:

1,1,2,3,5,8,13,21,34,55 ....

It is not difficult to find that the permutation of the sequence is: after a number plus the previous number, and so on;

If f0=0 and F1=1 so Fn=f (n-1) +f (n-2);

Here is a way to calculate the Fabonacci sequence (no recursion is used):

public long Fibonacci (int n) {

int a=1;

int b=1;

int n; Declare a variable to define the length of the sequence;

for (int i=2;i<n;i++) {

B=a+b; Get the value of B;

A=b-a; Get the value of a last time;

}

}

Recursive method:

public long Fabinacci (int n) {

if (n==0| | N==1) {//Meet condition

return n;

}

Return Fabinacci (i-2) +fabinacci (i-2); return value

}

We find by arranging that the Fabonacci sequence is actually the last number plus the previous number and

Fabonacci (n) =fabonacci (n-2) +fabonacci (n-1);

When the sequence elements are arranged from 0, the return value n is satisfied by the judging condition.

Then jump out of the following statement, has been circulating to meet the conditions of N, out of the exit to get the return value Fabonacci (n);

Conclusion: The essence of the method to call the method itself is shorthand for several methods;

It can call other methods, and it can call itself.

The recursion must have the export;

Cases:

public void Func1 (ref int num) {

if (num<3) {

num++;

FUNC2 (ref num);

}

}

public void FUNC2 (ref int num) {

if (num<3) {

num++;

FUNC3 (ref num);

}

}

public void Func3 (ref int num) {

if (num<3) {

num++;

FUNC4 (ref num);

}

}

......

Then we can simply abbreviate the above method:

public void Func (ref int num) {

if (num<3) {

num++;

Func (ref num);

}

}

C # recursion

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.