Todd Rowland
Wolfram Research
Maryland
Registered: Oct 2003
Posts: 103 |
Enumerating strings
Here is a function that people might find useful when performing enumerations. It enumerates all strings with k colors, of any length.
enum[n_,k_]:=With[{tmp=Length[IntegerDigits[(k-1) n+1,k]]-1}, IntegerDigits[n-(k^tmp -1)/(k-1),k,tmp]]
It does not have any gaps, and here is its inverse function:
unenum[ls_,k_]:=(k^Length[ls] - 1)/(k-1)+FromDigits[ls,k]
It relies on the formula for geometric sums, and the efficiency of the IntegerDigits function.
For comparison, some other enumeration functions compute
the strings of k colors of fixed length with IntegerDigits[n,k,len]
finite strings of arbitrary length and arbitrary colors with the Leibniz-Godel function (p.1120)
strings of fixed length with arbitrary colors with the pairing function
(p.1127 and the recent forum thread).
Report this post to a moderator | IP: Logged
|