What would be the best way to implement the Fannkuch benchmark in J?

>From the shootout page (http://tinyurl.com/f3wdz):

 a) Take a permutation of {1,...,n}, for example: {4,2,1,5,3}.
 b) Take the first element, here 4, and reverse the order of the first
    4 elements: {5,1,2,4,3}.
 c) Repeat this until the first element is a 1, so flipping won't
    change anything more: {3,4,2,1,5}, {2,4,3,1,5}, {4,2,3,1,5},
    {1,3,2,4,5}. 
 d) Count the number of flips, here 5.
 e) Do this for all n! permutations, and record the maximum number of
    flips needed for any permutation.

I came with the following immediate solution:

  a) and b)
  ---------
     fn =: {. (([EMAIL PROTECTED]@[ { ]) , }.) ]
     fn 4 2 1 5 3
  5 1 2 4 3

  c) and d)
  ---------
     c =: (>:@$:@fn)`0:@.({.=1:)"1
     c 4 2 1 5 3
  5

  e)
  --
     m =: >./@:c@([EMAIL PROTECTED] A. >:@i.)
     m 5
  7

However, "m 10" takes ages to compute compared to what is done in
other languages. What would be the best way to code this benchmark?

I'm surprised not to see J entries in the Shootout benchmark. J would
rock in terms of number of lines :)

  Sam
-- 
Samuel Tardieu -- [EMAIL PROTECTED] -- http://www.rfc1149.net/

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to