3

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
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
effbiae
  • 1,087
  • 1
  • 7
  • 22

3 Answers3

6

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
Simon Major
  • 291
  • 2
  • 7
2

The original Pascal triangle was indeed rotated by 45° compared to the modern textbook version, so OP's solution is correct but can be improved for readability:

q)sums\[6;6#1]
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

For those who enjoy deciphering obfuscated q code, here is a lambda-free solution:

q)p:(sums\).(-1+;#[;1])@\:
q)p 3
1 1 1
1 2 3
1 3 6
Alexander Belopolsky
  • 2,228
  • 10
  • 26
  • and from the ': solutions: ((+':),[;0]@)\[;1] -- from your link, Pascal's 'generator' (value 1) can be a parameter, too – effbiae Jul 29 '17 at 09:51
1

Heres a solution based off the fibonacci example on code.kx http://code.kx.com/q/ref/adverbs/

q)pascal:{{((+':)x),1}/[x;1]}
q)pascal 0
1
q)pascal 6
1 6 15 20 15 6 1

q){{((+':)x),1}\[x;1]}  7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
emc211
  • 1,369
  • 7
  • 14