Pascal's triangle: Difference between revisions

Updated second D version
(Updated second D version)
Line 495:
<lang d>import std.stdio, std.algorithm, std.range;
 
auto pascal(in int n) /*pure nothrow*/ {
return [1].recurrence!q{ zip(a[n - 1] ~ 0, 0 ~ a[n - 1])
auto p = [[1]];
.map!q{a[0] + a[1]}
foreach (_; 1 .. n)
p ~= zip(p[$-1] ~ 0, 0 ~ p[$-1]).map!q{a[0] + a[1]}() .array() };
return p;
}
 
void main() {
writeln(pascal.take(5)).writeln;
}</lang>
{{out}}
Output:
<pre>[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]</pre>
 
===Alternative Version===
There is similarity between Pascal's triangle and [[Sierpinski triangle]]. Their difference are the initial line and the operation that act on the line element to produce next line. The following is a generic pascal's triangle implementation for positive number of lines output (n).