Now that I realised you actually want the strings not just a number, this problem bugs me! I'm sure there's a way to simply manipulate the string into all its possible permutations.
I can't find any algorithm that wont loop before all the permutations have been found. You can get the first n!/(n-2)! permutations with this simple method:
Quote: "n$ = a$
i = 2 to len(a$)
swap 1st and ith characters of n$
when i = len(a$) : if n$ =/= a$ then repeat"
Example:
ABCDE (start)
BACDE (swap 1 and 2)
CABDE (swap 1 and 3)
DABCE (swap 1 and 4)
EABCD (swap 1 and 5)
AEBCD (swap 1 and 2)
BEACD (swap 1 and 3)
CEABD (swap 1 and 4)
DEABC (swap 1 and 5)
EDABC (swap 1 and 2)
ADEBC (swap 1 and 3)
BDEAC (swap 1 and 4)
CDEAB (swap 1 and 5)
DCEAB (swap 1 and 2)
ECDAB (swap 1 and 3)
ACDEB (swap 1 and 4)
BCDEA (swap 1 and 5)
CBDEA (swap 1 and 2)
DBCEA (swap 1 and 3)
EBCDA (swap 1 and 4)
ABCDE (swap 1 and 5)
oh poop! We've repeated the first string already!
Note that no matter what length string you take each line is unique! (until the first string is repeated.)
My Code:
do
input "Number of available characters:> ";n
input "String length:> ";k
p = factorial(n) / factorial(n-k)
gosub defineSourceString
w = permutations(a$,k)
print "Permutations found: "; w
print "Possible permutations: ";p
print "PRESS ANY KEY TO LOOP"
wait key
cls
loop
end
//
defineSourceString:
a$=""
for i = 1 to n
a$ = a$ + chr$(64+i)
next i
return
//
function permutations(a$,k)
n = len(a$)
w = 0
n$ = a$
`Will only find the first n!/(n-2)! permutations.
repeat
for x = 2 to n
inc w
oldn$ = n$
if w/k = int(w/k) then print left$(n$,k)
n$ = mid$(n$,x) + right$(left$(n$,x-1),x-2) + mid$(n$,1) + right$(n$,n-x)
next x
until n$ = a$
endfunction w
//
function factorial(n)
fn = 1
for k = 1 to n
if fn*k < fn then fn=0 : exit //integer overflow error (n! =/= 0)
fn = fn*k
next k
endfunction fn
It's very quick. If we can work out what "meta-manipulation" of the string is required to produce many groups like this that are all unique, then we'll have a very fast solution.