fannkuch and other shootout exercises

hi,

i’m looking for solutions to the?http://shootout.alioth.debian.org/?exercises. ?either in k4 or q.

i’m particularly looking for an efficient solution to the fannkuch exercise:

?http://shootout.alioth.debian.org/u32/performance.php?test=fannkuchredux#about

http://www.kx.com/q/bell.q has a solution for ackermann’s function?

?

ta, jack

Hi Jack,

here is my approach to fannkuch from nsl:
/ fannkuch
k)p:{1+$[0N!1<x;,/(>:'(x,x)#1,x#0)[;0,'1+p x-1];,!x]}
k)f:{G::0;{G+:1;@[x;i;:;|x i:!*x]}/x;F|:G-1}

q)p:{$[1<x;raze(idesc each (x,x)#1,x#0)[;0,/:1+p x-1];enlist til x]}
q)f:{-1+count {@[x;i;:;reverse x i:til first x]}scan x}
q)\t max f each 1+p 7

I replaced / for scan and removed globals setting.

I hope it’s what you need.

Best regards,
Bartosz

On Dec 19, 2011, at 4:50 AM, Bartosz Kaliszuk <bartosz.kaliszuk@gmail.com> wrote:

|/(#{?[x;*x;|:]})'1+!-8

The mysterious bit is:
?[x;*x;|:]

what rule does it follow? How does it apply ‘reverse’ to only n elements specified by ‘first x’?

if that’s the vector ternary, i think it’s taking x as a list of flags in the first argument, and where they are “true” (non-zero), replacing that address with first x; where false, replacing it with the symmetrically equivalent element from the other end of the list. note that an atom in the second or third argument will be vector extended to match the other argument.

It should be equivalent to

?[“b”$x;count#first x;reverse x]