[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 19:13:10 -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,

I'm no mathematician, but I think this will do what you want:

DIM 10 gString$(3628800)
dim as long gCount
dim as long r
xref chars(3628800,12) as char
end globals

local fn swapDigits(which,NextStr$)
dim as long d
long if which < 10
for d = which + 1 to 10
swap NextStr$[d],NextStr$[which]
gString$(r) = NextStr$
r++
fn swapDigits(which+1,NextStr$)
next
end if
end fn

gString$(0) = "0123456789"
r = 1
fn swapDigits(1,"0123456789")


 0"0
 =J= a  y
  "