JS: Recursive basis and examples-Fibonacci sequence, Yang Hui triangle

Source: Internet
Author: User

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

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.