You don't have to do the swaps, you could just calculate them: nn=. 2 4 1 3 >:i.<./(+:nn i. 1),+:nn i: 1 1 2 3 4 nn=. 2 3 4 5 1 6 7 8 >:i.<./(+:nn i. 1),+:nn i: 1 1 2 3 4 5 6 7 8
On Mon, Nov 26, 2012 at 10:19 PM, Dan Bron <j...@bron.us> wrote: > I added a solution to the task at RosettaCode. If we let J do the work > for us, the solution is fairly simple. We use ^:a: ("swop" until you hit > your fixed point, i.e. until 1={. ) and the count is the number of swops, > i.e. the length of the result. > > The task calls for calculating the "topswop" all possible permutations of > N integers (i.e. the maximal number of swops for any given permutation of > rank N). The task only calls for N up to 10, so brute force (calculating > all !N swops and taking the max) suffices. But for large N, brute force > would explode, so another method would be required. > > Just looking at the series, I suspect a simple arithmetical > (combinatorical) solution exists. It might even be ready-made in the OEIS. > But I'll leave that to you smart guys. > > -Dan > > > Please excuse typos; composed on a handheld device. > > On Nov 26, 2012, at 9:19 PM, David Ward Lambert <b49p23t...@stny.rr.com> > wrote: > > > Reference: http://rosettacode.org/wiki/Topswops > > What are good ways to count iterations? > > > > While=: 2 : 'u^:v^:_' > > > > topswop=: (|.@:{. , }.)~ {. > > > > topswop While (1 ~: {.) 2 4 1 3 > > 1 2 3 4 > > > > These ideas seem like structural nightmares: > > * separate data and counter with boxes; > > * append a counter to the data; > > * explicit code with a global noun; > > * +/@:(0 = (0,~topswop)While(1 ~: {.)) NB. <laughs>. > > > > The boxes or explicit code seem most general. > > > > Thank you, Dave. > > > > Bonus: find a definition topswop using C. > > > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- Devon McCormick, CFA ^me^ at acm. org is my preferred e-mail ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm