[futurebasic] Re: [FB] Permutation Challenge

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2000 : Group Archive : Group : All Groups

From: Jay Reeve <jktr@...>
Date: Sun, 26 Nov 00 22:51:16 -0600
>
>I've been trying to come up with an efficient function which will create a
>series of strings, each of which contains the numbers 0 - 9 in all possible
>permutations. The only proviso is that no number must be repeated in the
>string - every digit must be unique.
>
>DIM 10 String$(3628800)
>
>String$(n) = "0123456789"
>
>The function I've written is incredibly verbose and clunky, and I wondered
>if any of you mathematicians could come up with a small, efficient one.
>
Phil,

Oooh. Sorry about that first post. I was in a hurry. :0

Here is some code that is TESTED to do what you ask:
'====================
DIM 10 gString$(3628800)
dim gCount as long
end globals

local fn permutations(whichChar,NextStr$ as str15)
dim as long d
long if whichChar < 10
fn permutations(whichChar+1,NextStr$)
for d = whichChar + 1 to 10
swap NextStr$[d],NextStr$[whichChar]
gString$(gCount) = NextStr$
gCount++
fn permutations(whichChar+1,NextStr$)
next
end if
end fn

gString$(0) = "0123456789"
gCount = 1
fn permutations(1,gString$(0))
'====================

Now, let me ask, do you really need a list of every permutation, or could 
they be created on the fly?

 0"0
 =J= a  y
  "