Home > Article > Web Front-end > JavaScript code example to implement Yang Hui's triangle
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 ........
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>
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!