Roger Hui wrote:
> http://www.jsoftware.com/jwiki/Essays/Knight's_Tour
>
> ktour 8  now much faster, from 470 seconds to 12 seconds.
> Main loop from 17,739,768 iterations to 302,701 iterations.

Even faster:

kt=: 3 : 0
 if. 1>:y do. i.,~y return. end.
 m=. kmoves y
 p=. *:y
 stack=. ,&.>|.y (<:/[EMAIL PROTECTED] #&, * +/ ]) i.>.-:y
 while. #stack do.
  stack=./:~ stack  NB. line added
  s=. >{:stack
  if. a: e. (((i.p)-.s){m)-.&.><s do.
   if. (#s)=p-1 do. (,~y)$/:s,(i.p)-.s return. end.
   stack=. }:stack continue.
  end.
  stack=. (}:stack),(<s),&.>  s-.~({:s){::m
 end.
)

   time 'ktour 8'
12.7458
   time 'kt 8'
1.53991

Best wishes,

John


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

Reply via email to