Search code examples
kdb+q-langk

Pascal's triangle in k/q


fibonacci is nice:

  f:|+\
5 f\1 1

but i haven't found a similar expression for Pascal's Triangle. this is the best i can get:

q){x+\\x#1}6
1 1 1  1  1   1  
1 2 3  4  5   6  
1 3 6  10 15  21 
1 4 10 20 35  56 
1 5 15 35 70  126
1 6 21 56 126 252
1 7 28 84 210 462

Solution

  • There's a pretty nifty one on the q idioms page

    q)pt:{0+':x,0}
    q)4 pt\ 1
    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1