base_perm <- function(base, index = NULL, value = NULL) {
if(length(index) != 0) {
n
<- floor(logb
((base
-index
*(1-base
)), base
)) n_start <- (base-base^n)/(1-base)
aval <- lapply(1:n, function(x) return(0:(base-1)))
grid <- expand.grid(aval)
row <- index-n_start+1
return(cat(unlist(rev(grid[row,])), "\n"))
}
if(length(value) != 0) {
value <- rev(as.numeric(strsplit(value, "")[[1]]))
n <- length(value)
n_start <- (base-base^n)/(1-base)
aval <- lapply(1:n, function(x) return(0:(base-1)))
grid <- expand.grid(aval)
row <- 0
stop <- T
while(stop) {
row <- row+1
if(all(grid[row,] == value))
stop <- F
}
return(cat(row+n_start-1, "\n"))
}
}
base_perm(2, 54)
base_perm(2, value = "111000111")
YmFzZV9wZXJtIDwtIGZ1bmN0aW9uKGJhc2UsIGluZGV4ID0gTlVMTCwgdmFsdWUgPSBOVUxMKSB7CiAgaWYobGVuZ3RoKGluZGV4KSAhPSAwKSB7CiAgICBuIDwtIGZsb29yKGxvZ2IoKGJhc2UtaW5kZXgqKDEtYmFzZSkpLCBiYXNlKSkKICAgIG5fc3RhcnQgPC0gKGJhc2UtYmFzZV5uKS8oMS1iYXNlKQogICAgYXZhbCA8LSBsYXBwbHkoMTpuLCBmdW5jdGlvbih4KSByZXR1cm4oMDooYmFzZS0xKSkpCiAgICBncmlkIDwtIGV4cGFuZC5ncmlkKGF2YWwpCiAgICByb3cgPC0gaW5kZXgtbl9zdGFydCsxCiAgICByZXR1cm4oY2F0KHVubGlzdChyZXYoZ3JpZFtyb3csXSkpLCAiXG4iKSkKICB9CiAgaWYobGVuZ3RoKHZhbHVlKSAhPSAwKSB7CiAgICB2YWx1ZSA8LSByZXYoYXMubnVtZXJpYyhzdHJzcGxpdCh2YWx1ZSwgIiIpW1sxXV0pKQogICAgbiA8LSBsZW5ndGgodmFsdWUpCiAgICBuX3N0YXJ0IDwtIChiYXNlLWJhc2VebikvKDEtYmFzZSkKICAgIGF2YWwgPC0gbGFwcGx5KDE6biwgZnVuY3Rpb24o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