Definition: Programming skills called by the program itself are referred to as recursion. A procedure or function has a method of directly or indirectly invoking itself in its definition or description, which usually transforms a large and complex problem layer into a smaller problem similar to the original problem, and the recursive strategy can describe the repeated computations needed in the process of solving the problems by a small number of programs. Greatly reduces the code volume of the program.
Typically applied to a struct name call that is not a clear level.
Conditions that constitute recursion: 1. The sub-problem must be the same as the original problem, and more simple; 2. It is not allowed to call itself indefinitely, there must be an exit, and simplification is handled as a non-recursive condition. Example 1: Fibonacci sequence//Fibonacci sequence, also known as the Golden Section , refers to a sequence of such numbers: //1, 1, 2, 3, 5, 8, 13, 、......//Find the number of No. I<script>function SL (var i) {
if (i<=2) {
return 1;
} else{
Return SL (i-1) +SL (i-2); }
}</script>Example 2: Yang Hui triangle
1
1 1
1 2 1//nth row number of M: n=3, m=2, the number is 2;
1 3 3 1
1 4 6) 4 1
// ......Pleasenumber of first n rows m
<script>
function YHSJ (n,m) {
if (m>n) {
return false;
} else {
if (m==1 | | m==n) {
return 1;
} else {
Return YHSJ (n-1,m) + YHSJ (n-1,m-1);
}
}
}</script>
JS: Recursive basis and examples-Fibonacci sequence, Yang Hui triangle