This "perfect shuffle" algorithm can be implemented as: shuf=: /: $ /:@$ 0 1"_
The number of iterations needed to cycle back to the start for a permutation of length n is: *./#@>C.shuf i.n I'll update the rosettacode page with these details after it comes back up. Thanks, -- Raul On Mon, Jun 15, 2015 at 12:11 PM, David Lambert <[email protected]> wrote: > http://rosettacode.org/wiki/Perfect_shuffle#J > Using the "1 2 many" counting system, magic_cycle applied to the required > solution set runs for eternity allocating and copying memory most of which > is used only for the final tally. I've written an explicit version that > maintains the start and current deck as well as the count which runs in a > few minutes, not included to avoid clutter. > > Until =: conjunction def 'u^:(0-:v)^:_' > perfect_shuffle =: [: , [: |: ([\~ (0 _1r2 p. #)) > 'perfect shuffling check' assert 'adbecf' -: perfect_shuffle 'abcdef' > > Shuffles =: (@:{:)(,`)(`:6) NB. appends next state caused by a shuffling > verb > > f =: perfect_shuffle Shuffles > magic_shuffle =: f Until ({. -: {:) @: f @: ,: > > magic_shuffle 'abcdef' > abcdef > adbecf > aedcbf > acebdf > abcdef > > CYCLE=: (,. ([: <: ([: # [: magic_shuffle i.)"0)) +:>:i.5000]50 > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
