義大利人稱之為“塔塔利亞三角形”(Triangolo di Tartaglia)以紀念在16世紀發現一元三次方程解的塔塔利亞。
在歐洲直到1623年以後,法國數學家帕斯卡在13歲時發現了“帕斯卡三角”。
布萊士·帕斯卡的著作Traité du triangle arithmétique(1655年)介紹了這個三角形。帕斯卡蒐集了幾個關於它的結果,並以此解決一些機率論上的問題,影響面廣泛,Pierre Raymond de Montmort(1708年)和亞伯拉罕·棣·美弗(1730年)都用帕斯卡來稱呼這個三角形。
Private Sub Form_Click() N = InputBox("", "", 5) ReDim a(N + 1, N + 1), b(N + 1, N + 1) Cls k = 8 For I = 1 To N Print String((N - I) * k / 2 + 1, " "); For J = 1 To I a(I, 1) = 1 a(I, I) = 1 a(I + 1, J + 1) = a(I, J) + a(I, J + 1) b(I, J) = Trim(Str(a(I, J))) Print b(I, J); String(k - Len(b(I, J)), " "); Next J Print Next IEnd Sub
單擊視窗在彈出輸入框後輸入行數後按確認 就能在窗體上列印楊輝三角了
SQL
-- 使用組合的計算公式和階乘計算楊輝三角,對大數容易溢出。create function Factorial (@count int) returns int as begin declare @ret int,@index int set @ret = 1 --初始值為 1 set @index = 1 while(@index <= @count) begin set @ret = @ret * @index set @index = @index + 1 end return (@ret)endcreate function Combination (@num int,@count int) returns int as begin declare @up int,@L1 int,@R1 int,@ret int set @up = dbo.Factorial(@count) set @L1 = dbo.Factorial(@num) set @R1 = dbo.Factorial(@count - @num) set @ret = @up/(@L1 * @R1) return (@ret)endcreate function PrintRow (@num int) returns nvarchar(100) as begin declare @i int declare @str nvarchar(100) set @str = '' set @i = 1 while (@i < @num) begin set @str = @str + replace(str(dbo.Combination(@i,@num)),' ','') + ' ,' set @i = @i + 1 end return (@str)endcreate proc PasTrigLines(@num int) as begin -- 主程式 declare @i int set @i = 1 while(@i <= @num ) begin if (@i = 0 ) print 1 + ',' else if (@i = 1) print '1,1,' else print '1,' + dbo.PrintRow(@i) + '1,' set @i = @i + 1 endendexec PasTrigLines 10 -- 十個
public class TriangleArray{ public static void main(String[] args) { final int NMAX = 10; // allocate triangular array int[][] odds = new int[NMAX + 1][]; for (int n = 0; n <= NMAX; n++) odds[n] = new int[n + 1]; // fill triangular array for (int n = 0; n < odds.length; n++) for (int k = 0; k < odds[n].length; k++) { /* * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k) */ int lotteryOdds = 1; for (int i = 1; i <= k; i++) lotteryOdds = lotteryOdds * (n - i + 1) / i; odds[n][k] = lotteryOdds; } // print triangular array for (int[] row : odds) { for (int odd : row) System.out.printf("%4d", odd); System.out.println(); } }}
PHP
<?php/* 默認輸出十行,用T(值)的形式可改變輸出行數 */class T{ private $num; public function __construct($var=10) { if ($var<3) die("值太小啦!"); $this->num=$var; } public function display(){ $n=$this->num; $arr=array(); //$arr=array_fill(0,$n+1,array_fill(0,$n+1,0)); $arr[1]=array_fill(0,3,0); $arr[1][1]=1; echo str_pad(" ",$n*12," "); printf("%3d",$arr[1][1]); echo "<br/>"; for($i=2;$i<=$n;$i++){ $arr[$i]=array_fill(0,($i+2),0); for($j=1;$j<=$i;$j++){ if($j==1) echo str_pad(" ",($n+1-$i)*12," "); printf("%3d",$arr[$i][$j]=$arr[$i-1][$j-1]+$arr[$i-1][$j]); echo " "; } echo"<br/>"; } }}$yh=new T(); //$yh=new T(數量);$yh->display();?>
# -*- coding: utf-8 -*-#!/usr/bin/env pythondef triangles(): L = [1] while True: yield L L = [sum(i) for i in zip([0]+L, L+[0])]
該方式用到了列表生成式,理解起來較困難,下面是另一種方式:
def triangles(): ret = [1] while True: yield ret for i in range(1, len(ret)): ret[i] = pre[i] + pre[i - 1] ret.append(1) pre = ret[:]
另一個不用生成器的版本:
def YangHui (num = 10): LL = [[1]] for i in range(1,num): LL.append([(0 if j== 0 else LL[i-1][j-1])+ (0 if j ==len(LL[i-1]) else LL[i-1][j]) for j in range(i+1)]) return LL