Results 1 to 26 of 26

Threaded View

  1. Collapse Details
    Pascal's triangle 
    #1
    fams casino syn's Avatar
    Join Date
    Dec 2011
    Location
    flavor town
    Posts
    3,771
    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;
    }
    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.
    Opinions? Post you're own solutions.
    Last edited by syn; 01-06-2012 at 06:05 AM.
    Reply With Quote
     

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •