This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -6,3 +6,6 @@
where each element of each row is either 1 or the sum of the two elements right above it. For example, the next row would be 1 (since the first element of each row doesn't have two elements above it), 4 (1 + 3), 6 (3 + 3), 4 (3 + 1), and 1 (since the last element of each row doesn't have two elements above it). Each row <tt>n</tt> (starting with row 0 at the top) shows the coefficients of the binomial expansion of (x + y)<sup>n</sup>.
Write a function that prints out the first n rows of the triangle (with <tt>f(1)</tt> yielding the row consisting of only the element 1). This can be done either by summing elements from the previous rows or using a binary coefficient or combination function. Behavior for <tt>n <= 0</tt> does not need to be uniform, but should be noted.
'''See also:'''
* [[Evaluate binomial coefficients]]

View file

@ -1,12 +1,11 @@
import std.stdio, std.algorithm, std.range;
auto pascal(in int n) /*pure nothrow*/ {
auto p = [[1]];
foreach (_; 1 .. n)
p ~= zip(p[$-1] ~ 0, 0 ~ p[$-1]).map!q{a[0] + a[1]}().array();
return p;
auto pascal() /*pure nothrow*/ {
return [1].recurrence!q{ zip(a[n - 1] ~ 0, 0 ~ a[n - 1])
.map!q{a[0] + a[1]}
.array };
}
void main() {
writeln(pascal(5));
pascal.take(5).writeln;
}