SRL Archive - Other Scripts

Pascals Triangle


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.

Dan Cardin

2008-10-15 18:11:39

nifty, im too lazy to do it myself :)

*whispers to mixster and tells him to make it better (if you know what i mean ;))*

Shuttleu

2008-10-15 18:23:47

ermmm… what is pascals triangle?

~shut

Narcle

2008-10-15 18:30:08

From: Shuttleu
ermmm… what is pascals triangle?

~shut


SCAR = Pascal …

Shuttleu

2008-10-15 18:33:03

From: Narcle
SCAR = Pascal …

i know that but what is the triangle for? or what does it do?

~shut

Lee Lok Hin

2008-10-15 18:38:59

http://en.wikipedia.org/wiki/Pascal_triangle#Patterns_and_properties