Base 3 with digits instead of . Every integer — positive or negative — has a unique representation, with no sign needed.
Conversion
vector<int> toBalancedTernary(long long n) {
vector<int> d;
while (n) {
int r = ((n % 3) + 3) % 3;
if (r == 2) { d.push_back(-1); n = (n + 1) / 3; } // 2 -> -1, carry +1
else { d.push_back(r); n = (n - r) / 3; }
// careful with negatives: use floor division
}
return d; // least significant first
}The rule: take ; if it is 2, record and carry 1 into the next digit.
5 = 9 - 3 - 1 = (1, -1, -1)
-5 = -9 + 3 + 1 = (-1, 1, 1)
7 = 9 - 3 + 1 = (1, -1, 1)
The classic application: the balance scale
With weights and a two-pan balance, you can weigh any integer mass.
Because each weight can go on the empty pan (coefficient ), on the object’s pan (), or be unused () — exactly the three balanced-ternary digits. With weights up to you can measure every mass from 1 to .
This is Bachet’s weight problem, and it is the canonical reason the representation is worth knowing.
Properties
| Property | |
|---|---|
| Representation is unique | for every integer, positive or negative |
| Negation = flip every digit’s sign | no two’s-complement asymmetry |
| No separate sign bit | the leading digit carries it |
| Rounding = truncation | truncating always gives the nearest value |
| Range with digits | — symmetric |
| Digit count for |
The symmetric range and free negation are why balanced ternary is occasionally called “the most elegant number system”. The Soviet Setun computer (1958) used it.
Where it appears in problems
| Problem | Why balanced ternary |
|---|---|
| ”Weigh any mass with weights “ | the defining example |
| ”Represent as with distinct exponents” | exactly the representation |
| ”Can be made from powers of 3, each used at most once, with ?“ | always yes, and uniquely |
| Games where each move is | state = balanced ternary digits |
| Ternary search trees / three-way partitioning | the three-outcome analogy |
| ”Minimum number of powers of 3 summing to with signs” | popcount of the non-zero digits |
Generalisation: non-adjacent form (NAF)
The base-2 analogue uses digits with the rule that no two adjacent digits are non-zero. Every integer has a unique NAF, and it has the minimum possible number of non-zero digits — on average instead of .
vector<int> naf(long long n) {
vector<int> d;
while (n) {
if (n & 1) { int z = 2 - (int)(n % 4); d.push_back(z); n -= z; }
else d.push_back(0);
n >>= 1;
}
return d;
}This makes modular exponentiation and elliptic curve scalar multiplication about 11% faster, because each non-zero digit costs a multiplication and NAF has fewer of them. It is the standard optimisation in cryptographic implementations.
Related representations
| System | Digits | Note |
|---|---|---|
| Binary | the default | |
| Balanced ternary | base 3 | symmetric, free negation |
| NAF | base 2 | minimal weight |
| Zeckendorf | , Fibonacci base | no two adjacent 1s |
| Factorial base | digit in | permutation ranking |
| Gray code | consecutive values differ in one bit | |
| Negabinary | base | represents negatives without a sign bit |
Factorial base is worth knowing alongside: it is how you convert between a permutation and its lexicographic rank, via the Lehmer code.
See also: Gray Code · Binary Exponentiation · General Number Theory