Permutations
#####
#####
=================================================
            P E R M U T A T I O N S
=================================================

PERMUTATIONS generates and prints all the
permutations of a string of 1 to 10 characters.

Alex Measday
www.geonius.com/
    writing/articles/permutations.html

=================================================

Setting Up:
-----------

Initialize the total # of permutations printe
(R0) to zero:

CLR STO 00

Initialize the character loop index (R6) to N-1,
where N is the length of the string:

<length-1> STO 06

Specify the length N (R7) of the string:

<length> STO 07

Initialize the stack pointer (R8) to point to the
first entry (memory register 9) on the stack:

9 STO 08

Specify the N character indices (1-9 and 0) of
the initial string.  Enter an N-digit number,
where N is the length of the string and each
digit is the offset from memory register 20 of
the memory register (20-29) containing that
character's printer code.  If the string length
is less than 10, then index/digit 0 always
represents a blank character.  Store the
character indices number in the first entry on
the stack:

<1-10 digits> STO 09

For each index, 0-9, store the corresponding
character's printer code in memory registers
20-29.  If the string length is less than 10,
then index 0 always represents a blank character
and printer code 00 must be stored in memory
register 20.

CLR STO 20              // If string length < 10
<2-digit code> STO 21   // First distinct char
<2-digit code> STO 22   // Second distinct char
...

-------------------------------------------------

Running the Program:

R/S

The program will print out the permutations at
irregular intervals.

=================================================

Example:
--------

The "permutations_01.wri" file has the memory
registers preset for generating and printing all
the permutations of "TRY IT", a 6-character
string with 4 distinct characters plus the blank.

The memory registers were initialized as follows:

CLR STO 00     // Permutation count = 0

5 STO 06       // Loop index = N - 1 = 5
6 STO 07       // String length N = 6

9 STO 08       // Stack pointer points to R9
123041 STO 09  // Indices for initial string:
               // 1 = T, 2 = R, 3 = Y, 4 = I

CLR STO 20     // ' ' printer code = 00
37 STO 21      // 'T' printer code = 37
35 STO 22      // 'R' printer code = 35
45 STO 23      // 'Y' printer code = 45
24 STO 24      // 'I' printer code = 24

Press R/S to run the program.  A 6-character
string always generates 720 permutations.  The
program does not prune duplicate permutations.
Because "TRY IT" has two Ts, there will be
720/2 = 360 duplicate permutations.

=================================================
