Home > Web Front-end > JS Tutorial > JavaScript code example to implement Yang Hui's triangle

JavaScript code example to implement Yang Hui's triangle

不言
Release: 2019-01-17 09:51:59
forward
4676 people have browsed it

This article brings you code examples about implementing Yang Hui triangle in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

JavaScript to implement Yang Hui's triangle

       1 
      1 1 
     1 2 1 
    1 3 3 1 
   1 4 6 4 1 
 1 5 10 10 5 1
........
Copy after login

Observe such a set of numbers, find out the pattern, and use the console to output such regular numbers

Rule:

This is Yang Hui's triangle. The numbers at the beginning and end of each row are 1, and the remaining numbers are the addition of the corresponding numbers in the previous row. Consider using a recursive algorithm.

Answer reference:

function combination(m,n){
    if(n == 0) return 1;//第一个数为1
    else if(m == n) return 1; //最后一个数为1
    else return combination(m-1,n-1)+combination(m-1,n);//中间的数为前一行的两个数相加
}
function Print(n){ 
    for( var i = 0 ; i <p><span class="img-wrap"><img src="https://img.php.cn//upload/image/838/629/233/1547689738622346.png" title="1547689738622346.png" alt="JavaScript code example to implement Yang Huis triangle"></span></p><p class="comments-box-content"></p>
Copy after login

The above is the detailed content of JavaScript code example to implement Yang Hui's triangle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template