Pascal's triangle: Difference between revisions
Content added Content deleted
Line 314: | Line 314: | ||
auto pascal(int n) { |
auto pascal(int n) { |
||
auto p = [[1]]; |
auto p = [[1]]; |
||
foreach (_; |
foreach (_; 1 .. n) |
||
p ~= array(map!q{a[0] + a[1]}(zip(p[$-1] ~ 0, 0 ~ p[$-1]))); |
p ~= array(map!q{a[0] + a[1]}(zip(p[$-1] ~ 0, 0 ~ p[$-1]))); |
||
return p; |
return p; |
||
Line 323: | Line 323: | ||
}</lang> |
}</lang> |
||
Output: |
Output: |
||
<pre>[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4 |
<pre>[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]</pre> |
||
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). |
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). |
||
<lang d>import std.stdio, std.string, std.format : fmx = format; |
<lang d>import std.stdio, std.string, std.format : fmx = format; |