Motivated by the Hacker News challenge in http://news.ycombinator.com/item?id=3429466 I just made this.
Code:#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; }Opinions? Post you're own solutions.Code: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.
Thread: Pascal's triangle
Results 1 to 26 of 26
Threaded View
-
01-06-2012
Last edited by syn; 01-06-2012 at 06:05 AM.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)