mixster
2008-10-15 16:37:04
program PascalsTriangle;
var
p, n: TIntegerArray;
i, x: Integer;
s: string;
begin
p := [1];
for i := 1 to 16 do
begin
SetArrayLength(n, High(p) + 2);
s := '';
for x := 0 to High(n) do
begin
if (x = 0) or (x = High(n)) then
begin
n[x] := 1;
Continue;
end;
n[x] := p[x] + p[x - 1];
s := s + IntToStr(n[x]) + ' ';
end;
Writeln(s);
SetArrayLength(p, High(n) + 1);
for x := 0 to High(p) do
p[x] := n[x];
end;
end.I'm disappointed it was easier than I thought. Only problem was I had p[x + 1] for one loop before realising it was -1 :(
Edit: Created one using formula from Wikipedia - can now generate single rows rather than all before
program PascalsTriangle;
var
n: TIntegerArray;
c, r: Integer;
s: string;
begin
for r := 16 to 16 do
begin
SetArrayLength(n, 0);
SetArrayLength(n, r + 1);
n[0] := 1;
for c := 1 to High(n) do
n[c] := (n[c - 1] *((r + 1) - c)) / c;
for c := 0 to High(n) do
s := s + IntToStr(n[c]) + ' ';
Writeln(s);
s := '';
end;
end.