Last active
September 29, 2020 01:39
-
-
Save Anaminus/ced9c7bacc1281ed5aacb1f2ec9cc83d to your computer and use it in GitHub Desktop.
Like a permutation, but also includes subsets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function NthSubPerm(v, b) | |
| local B = #b | |
| if B <= 1 then return b:rep(v+1) end | |
| local p = math.floor(math.log((v+2)*(B-1))/math.log(B))-1 | |
| v = v - math.modf(B*(B^p-1)/(B-1)) | |
| local s = {} | |
| for i = 0, p do | |
| local j = math.modf(v / B^i % B) | |
| s[i+1] = b:sub(j+1,j+1) | |
| end | |
| return table.concat(s) | |
| end | |
| for i = 0, 40 do | |
| print(i, NthSubPerm(i,"AB"), NthSubPerm(i,"ABC")) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment