Dustin
01-06-2012, 06:57 AM
Motivated by the Hacker News challenge in http://news.ycombinator.com/item?id=3429466 I just made this.
#include <stdio.h>
#include <stdlib.h>
#define NTH_IN_ROW(r, n) (((r) * (r) + (r)) / 2 + (n))
int main() {
long *a, i, j, r;
scanf("%ld", &r);
a = malloc(sizeof(long) * ((r + 1) * (r + 1) + r + 1) / 2);
for (i = 0; i <= r; i++) {
a[NTH_IN_ROW(i, 0)] = 1;
a[NTH_IN_ROW(i, i)] = 1;
if (i > 1)
for (j = 1; j < i; j++)
a[NTH_IN_ROW(i, j)] =
a[NTH_IN_ROW(i - 1, j)] +
a[NTH_IN_ROW(i - 1, j - 1)];
}
for (j = 0; j <= r; j++)
printf("%ld ", a[NTH_IN_ROW(r, j)]);
putchar('\n');
return 0;
}
program Pascal;
var counter as integer;
var ptLine as integer[20];
procedure Step(istep as integer);
var couter as integer;
begin
ptLine[istep] := 1;
if (istep<>1) then write( " " + ptLine[1]);
if (istep>=3) then begin
for couter := 2 to istep-1 do begin
ptLine[couter] = ptLine[couter] + ptLine[couter-1];
write(" " + ptLine[couter]);
end;
end;
writeln(" " + ptLine[istep]);
end;
begin
for counter:=1 to 20 do begin
Step(counter);
end;
end.
Opinions? Post you're own solutions.
#include <stdio.h>
#include <stdlib.h>
#define NTH_IN_ROW(r, n) (((r) * (r) + (r)) / 2 + (n))
int main() {
long *a, i, j, r;
scanf("%ld", &r);
a = malloc(sizeof(long) * ((r + 1) * (r + 1) + r + 1) / 2);
for (i = 0; i <= r; i++) {
a[NTH_IN_ROW(i, 0)] = 1;
a[NTH_IN_ROW(i, i)] = 1;
if (i > 1)
for (j = 1; j < i; j++)
a[NTH_IN_ROW(i, j)] =
a[NTH_IN_ROW(i - 1, j)] +
a[NTH_IN_ROW(i - 1, j - 1)];
}
for (j = 0; j <= r; j++)
printf("%ld ", a[NTH_IN_ROW(r, j)]);
putchar('\n');
return 0;
}
program Pascal;
var counter as integer;
var ptLine as integer[20];
procedure Step(istep as integer);
var couter as integer;
begin
ptLine[istep] := 1;
if (istep<>1) then write( " " + ptLine[1]);
if (istep>=3) then begin
for couter := 2 to istep-1 do begin
ptLine[couter] = ptLine[couter] + ptLine[couter-1];
write(" " + ptLine[couter]);
end;
end;
writeln(" " + ptLine[istep]);
end;
begin
for counter:=1 to 20 do begin
Step(counter);
end;
end.
Opinions? Post you're own solutions.