Stats

Popular Posts

Followers

Generate the Pascal triangle with mathematica

養花種魚數月亮賞星星 於 2008年12月15日星期一 下午5:20 發表
mathematica;教學;pdf mathematica 6教學 mathematica基本教學
Write a function "pascal" would produce the (nested) list "{{1}, {1,1}, {1,2,1}, {1,3,3,1}....}".

Generate the triangle by addition; do not use the function "Binomial[ ]".

pascal[n_Integer]:=NestList[Insert[#,0,1]+Insert[#,0,Length[#]+1]&,{1},n]

or

pascal[n_Integer]:=Module[{t},t=n;
aa=NestList[Insert[#,0,1]+Insert[#,0,Length[#]+1]&,{1},t+1];
bb=Table[Insert[aa[[i]],"",Table[{m},{m,2,i}]],{i,1,t+1}];
Table[Nest[Insert[#,"",1]&,bb[[i]],t+1-i],{i,1,t+1}]//TableForm]



Tags:

讀者回應 ( 0 意見 )

發佈留言

Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.

如果這篇文章對你有幫助,那請留個訊息給我~