Balanced Ternary

Definition

Balanced ternary, also called symmetric ternary, is a generalized positional numeral system.

The digits of ordinary ternary consist of 0,1, and 2, while the digits of balanced ternary consist of -1,0, and 1. Its base is also 3 (because there are three possible values). Since writing -1 as a digit is inconvenient, we use the letter Z to represent -1.

Explanation

Here are several examples:

DecimalBalanced ternaryDecimalBalanced ternary
0051ZZ
1161Z0
21Z71Z1
310810Z
4119100

Representing negative numbers in this numeral system is easy: simply reverse the digits of the positive number (Z becomes 1, and 1 becomes Z).

DecimalBalanced ternary
-1Z
-2Z1
-3Z0
-4ZZ
-5Z11

It is easy to see that the most significant digit of a negative number is Z, while that of a positive number is 1.

Process

In the conversion method for balanced ternary, first write a given number x in ordinary ternary. When x is represented in ordinary ternary, each digit is 0, 1, or 2. Starting from the least significant digit, we can skip any 0 and 1, but if we encounter 2, we should first change it to Z and add 1 to the next digit. When we encounter digit 3, it should be changed to 0 and 1 should be added to the next digit.

Application 1

Convert 64 to balanced ternary.

First, rewrite the number in ordinary ternary:

6410=021013\text 64_{10} = 02101_3

Let us process the digits with the smallest effect on the whole number first (the least significant digit):

  • 101 is skipped (because 0 and 1 are allowed in balanced ternary);
  • 2 becomes Z, and the digit to its left is increased by 1, giving 1Z101;
  • 1 is skipped, giving 1Z101.

The final result is 1Z101.

We then convert it back to decimal:

1Z101=81×1+27×(−1)+9×1+3×0+1×1=6410\texttt {1Z101}=81 \times 1 +27 \times (-1) + 9 \times 1 + 3 \times 0 + 1 \times 1 = 64_{10}

Application 2

Convert 237 to balanced ternary.

First, rewrite the number in ordinary ternary:

23710=222103\text 237_{10} = 22210_3
  • 0 and 1 are skipped (because 0 and 1 are allowed in balanced ternary);
  • 2 becomes Z, and the digit to its left is increased by 1, giving 23Z10;
  • 3 becomes 0, and the digit to its left is increased by 1, giving 30Z10;
  • 3 becomes 0, and the digit to its left (defaulting to 0) is increased by 1, giving 100Z10;
  • 1 is skipped, giving 100Z10.

The final result is 100Z10.

We then convert it back to decimal:

100Z10=243⋅1+81⋅0+27⋅0+9⋅(−1)+3⋅1+1⋅0=23710\mathtt{100Z10} = 243 \cdot 1 + 81 \cdot 0 + 27 \cdot 0 + 9 \cdot (-1) + 3 \cdot 1 + 1 \cdot 0 = 237_{10}

Properties

For a balanced ternary number X3X_3, multiplying each digit xix_i by its corresponding weight 3i3^i uniquely produces a decimal number Y10Y_{10}.

For a decimal number Y10Y_{10}, does it uniquely correspond to one balanced ternary number?

The answer is yes. This property is called the uniqueness of balanced ternary.

Proof

We use proof by contradiction:

Suppose a decimal number Y10Y_{10} has two different balanced ternary numbers A3,B3A_3,B_3 that convert to Y10Y_{10}; that is, we need to prove A3=B3A_3 = B_3. We discuss cases:

  1. When Y10=0Y_{10}=0, clearly A3=B3=03A_3 = B_3 = 0_3, contradicting the assumption.

  2. When Y10>0Y_{10}>0:

    • Number the digits of A3A_3 and B3B_3 from low to high, and let aia_i be the digit of A3A_3 at position ii, and bib_i be the digit of BB at position ii. In A3,B3A_3,B_3, there must be an ii such that ai≠bia_i\neq b_i. It can be seen that digits i−1,i−2,…,0i-1,i-2,\dots,0 are irrelevant to the proof. Therefore, shift A3,B3A_3,B_3 right by ii digits to obtain A3′,B3′A_3',B_3'; the original problem is equivalent to proving A3′=B3′A_3'=B_3'.
    • For A3′,B3′A_3',B_3''s digit 00, a0≠b0a_0 \neq b_0. Assume b0>a0b_0 > a_0 (the result is the same when a0>b0a_0>b_0). Clearly b0−a0∈{1,2}b_0 - a_0 \in \{1,2\}. For A3′A_3', the contribution of its digits i=1,2,3,…i=1,2,3,\dots to A3′A_3''s value is S1=a1×31+a2×32+…S_1 = a_1 \times 3^1 + a_2 \times 3^2+ \dots, and for B3′B_3', the contribution of its digits i=1,2,3,…i=1,2,3,\dots to B3′B_3''s value is S2=b1×31+b2×32+…S_2 = b_1 \times 3^1 + b_2 \times 3^2 + \dots. Since A3′=B3′A_3' = B_3', we obtain S1−S2=b0−a0S_1 - S_2 = b_0 - a_0. S1,S2S_1,S_2 have a common factor of 33, while b0−a0b_0 - a_0 is not divisible by 33, contradicting the assumption. Therefore, A3′≠B3′A_3'\neq B_3'.
  3. When Y10<0Y_{10}<0, the proof is the same as for Y10>0Y_{10}>0.

Thus, every decimal Y10Y_{10} has a unique corresponding balanced ternary X3X_3.

Practice Problem

Topcoder SRM 604 PowerOfThree

Part of this page is adapted from the blog post Троичная сбалансированная система счисления and its English translation Balanced Ternary. The Russian version is licensed under Public Domain + Leave a Link; the English version is licensed under CC-BY-SA 4.0.

This content comes from OI Wiki: Balanced Ternary. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.