Home > Net >  Looking for a way of converting terniary to decimal
Looking for a way of converting terniary to decimal

Time:02-02

So i need to convert a terniary bumber as

terniary <- c(1001000, 121)

To obtain something like:

> decimal
[1] 756 16 

CodePudding user response:

One option:

fTert <- function(x) {
  place <- 0:(ceiling(log10(max(x))) - 1)
  colSums(3^place*(round(outer(10^-place, x)) %% 10))
}

fTert(c(1001000, 121))
#> [1] 756  16

A version that will take decimals:

fTert <- function(x) {
  place <- (-match(TRUE, colSums(matrix(round(x, rep(1:(max(nchar(x)) - 1), each = length(x))), nrow = length(x)) == x) == length(x))):(ceiling(log10(max(x))) - 1)
  colSums(3^place*(round(outer(10^-place, x)) %% 10))
}

fTert(c(100100.1, 1.201, 112.02))
#> [1] 252.333333   1.703704  14.222222

CodePudding user response:

Try the code below

f <- Vectorize(function(x, base = 3) {
  ifelse(x < base, x, Recall(x %/% 10) * base   x %% 10)
})

which gives

> f(terniary)
[1] 756  16
  •  Tags:  
  • Related