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

Reply via email to