kdb+

Integer pyramid

How would you create such an output in q:

,0
0 1
0 1 2
0 1 2 3
0 1 2 3 4

Probably your solution would be like this:

til each 1+til 5

But there is also other way which is even faster:

enlist[enlist 0],0,\1+til 4

Well, at least my PC states that it is faster ;)

q> \t do[1000;enlist[enlist 0],0,\1+til 400]
98
q> \t do[1000;til each 1+til 401]
171

Even Nicer Solution

Instead of complicated

enlist[enlist 0],0,\1+til 400

use short and sweet

(,\) til 401

Thanks, Rs!

5 thoughts on “Integer pyramid”

    1. Thanks Ajay for another solution! We are almost as good as ancient Egyptians ;-) However, isn’t this one slower?

Leave a comment