(subscriptrange): topswap: procedure options (main); /* 12 November 2013 */ declare cards(*) fixed (2) controlled, t fixed (2); declare dealt(*) bit(1) controlled; declare (count, i, m, n, c1, c2) fixed binary; declare random builtin; do n = 1 to 10; allocate cards(n), dealt(n); /* Take the n cards, in order ... */ do i = 1 to n; cards(i) = i; end; /* ... and shuffle them. */ do i = 1 to n; c1 = random*n+1; c2 = random*n+1; t = cards(c1); cards(c1) = cards(c2); cards(c2) = t; end; /* If '1' is the first card, game is trivial; swap it with another. */ if cards(1) = 1 & n > 1 then do; t = cards(1); cards(1) = cards(2); cards(2) = t; end; count = 0; do until (cards(1) = 1); /* take the value of the first card, M, and reverse the first M cards. */ m = cards(1); do i = 1 to m/2; t = cards(i); cards(i) = cards(m-i+1); cards(m-i+1) = t; end; count = count + 1; end; put skip edit (n, ':', count) (f(2), a, f(4)); end; end topswap;