効率的な一般的なショートカットにつながる数学的特性はないと思います。XOR が基数 2 で機能する理由は、XOR がキャリー ディスカウントを伴う加算であるという便利な性質を持っているためです。
単純な再帰関数でアルゴリズムを適用できます。たとえば、基数変換に Scala の BigInt クラスを利用します。
def sums(radix: Int, digits: List[List[String]]): String =
if(digits exists { _.nonEmpty }) // there's at least 1 bit left to add
(digits.flatMap { _.headOption } // take the 1st bit of all numbers
.map { BigInt(_, radix) } // convert to int representation
.sum
.toInt % radix // modulo by base
).toString +
sums(radix, digits map { _.drop(1) }) // do next most significant bit
else
"" // base case: no digits left to add
def sum(radix: Int, ns: List[Int]): Int =
BigInt(
sums(
radix,
ns // use BigInt to convert from int representation to string
.map { BigInt(_) }
.map { _.toString(radix).split("").drop(1).toList.reverse }
)
.reverse,
radix
).toInt
scala> sum(3, List(4,4,4,2))
res0: Int = 2
あなたの質問には「パフォーマンス」というタグが付けられていますが、改善されたアプローチを知らせるために、メモリやランタイムに関する追加の制約はありません。