M
M
MrChen2016-12-11 02:27:57
Python
MrChen, 2016-12-11 02:27:57

How to convert a number from decimal to balanced ternary?

Hello! Help solve the problem, please. Please explain how exactly to transfer.247d53e5ee9a4cce870ab01ccf4d659c.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2016-12-11
@OLS

1) Convert the number to the "ordinary" ternary number system - and if the number is negative, then add a minus to all digits
2) go through the digits from right to left: replace all (2) with ($) and add +1 to the higher digit; we replace all (3) with (0) and add (+1) to a higher digit; we replace all (-2) with (1) and add (-1) to a higher digit; we replace all (-3) with 0 and add (-1) to the higher bit.
"Positive" example : 71 (underscore means loop counter location)

71 = 27*2 + 9*1 + 3*2 + 1*2
   2 1 2 2<
   2 1 3<$
   2 2<0 $
   3<$ 0 $
 1<0 $ 0 $
<1 0 $ 0 $
Checking: 81 - 9*1 - 1 = 71
"Negative" example: -71
-71 = 27*(-2) + 9*(-1) + 3*(-2) + 1*(-2)
   -2 -1 -2 -2<
   -2 -1 -3< 1
   -2 -2< 0  1
   -3< 1  0  1
 $< 0  1  0  1
<$  0  1  0  1
Checking: 81*(-1) + 9*1 + 1*1 = -71

M
Mikhail Usotsky, 2016-12-11
@AquariusStar

In fact, the following negative unit sign is adopted: ͞1. Brusentsov also wrote this. And it's easy to translate. An asymmetric ternary system can be converted into a symmetrical one by setting: 2 → 1͞1. That is, we translate the number 7 as 21, and then arithmetically as 1͞10 + 001 = 1͞11. Let's take the number 14 as an example and translate it into a symmetrical and balanced system. We get the number 14 in the asymmetric system as 112, then we get the following: 110 + 01͞1 = 1͞1͞1͞1. Or 17 is 122 in asymmetrical or 1͞10͞1 in symmetrical. For negative it is implemented very simply. First we reserve a negative value. Let's transform the number as positive, and at the end of the series of trits, we will invert it due to the negative sign. For example: -17 is ͞1111. This is how it is decided.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question