ForHosting KIT · Math

Balanced ternary converter

20 in balanced ternary is 1 −1 1 −1: three cubed, minus three squared, plus three, minus one. Enter any integer, negative ones included, to get its signed-digit form with clear spacing between the digits. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
N
20
You get
Balanced ternary
1 -1 1 -1
Digits
1, -1, 1, -1

The balanced ternary converter rewrites an integer in base three while allowing each digit to be minus one, zero, or one. Enter a positive, negative, or zero integer and receive both a readable space-separated representation and a numeric digit array. The spacing keeps minus-one digits unambiguous when you copy the result into notes, code, or a calculation. The conversion is exact, deterministic, and performed with integer arithmetic, so it is useful for studying signed-digit number systems, checking exercises, and preparing small test vectors without doing repeated division by hand.

What balanced ternary represents

Ordinary ternary uses the digits zero, one, and two, with each position worth a power of three. Balanced ternary replaces two with minus one, giving the digit set -1, 0, and 1. A digit still multiplies the power of three at its position, but negative digits can subtract that place value. For example, the displayed sequence is ordered from the highest power to the units position, just as it is in familiar decimal notation.

Because the token -1 occupies one position, this converter separates digits with spaces instead of compressing them into a string that could be misread. The returned digits array provides the same information in a form programs can consume directly. Every integer has a unique finite balanced ternary expansion when leading zeros are excluded.

Positive and negative inputs therefore need no separate sign character: the signs already live inside the digits themselves. This makes the representation symmetrical around zero and especially convenient when comparing an integer with its negation.

How the conversion algorithm works

The converter repeatedly divides the current integer by three and examines the remainder. A remainder of zero or one is already a permitted balanced digit, so it is recorded directly. A remainder of two is replaced by -1 and a carry of one is added to the next position, since two equals minus one plus one group of three.

JavaScript can produce negative remainders for negative dividends, so the mirror case is handled too: a remainder of -2 becomes 1 and the next quotient is reduced by one. Digits are discovered from the units place upward and reversed at the end for normal reading order. The loop stops only when the adjusted quotient becomes zero.

This procedure uses no approximations, lookup service, random value, or clock. Rejecting those values prevents a plausible-looking but incorrect digit sequence from being returned.

Reading, checking, and using the result

To check a result, start at the rightmost digit with the power three to zero, multiply each digit by its corresponding power of three, and add the signed terms. The sum must reproduce the original integer. The original `n` is repeated so logs and batches remain self-describing.

Zero is the sole special presentation case and returns one zero digit rather than an empty sequence. Use the browser tool for quick exploration, classroom work, or checking a single calculation.

Check number-system exercises

Convert an assigned decimal integer and compare the signed digits with a hand-worked repeated-division solution.

Explore symmetric representations

Compare positive and negative integers to see how signed digits express negation without a separate sign prefix.

What digits does balanced ternary use?

It uses -1, 0, and 1. Each position has a place value that is a power of three.

Why are the digits separated by spaces?

Spacing makes -1 a clear single digit and prevents its minus sign from being confused with a sign for the entire number.

Can the converter handle negative integers?

Yes. Negative values are represented directly with signed digits; no separate leading sign is added.

Why are very large integers rejected?

Rejecting them protects the exactness of the result.

How do I check a balanced ternary result by hand?

Start at the rightmost digit, the units place, multiply each digit by its power of three and add the signed terms. For 1 −1 1 −1 that is three cubed minus three squared plus three minus one, which gives 20.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/numth/balanced-ternary

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/numth/balanced-ternary \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"n":20}'
{
  "n": 20
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "numth.balanced_ternary",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

How the conversion algorithm works

The input must be a safe integer because larger JSON numbers may already have lost precision before conversion begins.

Reading, checking, and using the result

The `balanced_ternary` field is intended for people: its tokens are separated by single spaces, and each token is exactly -1, 0, or 1. The `digits` field is intended for software and lists those same values as JSON integers from most significant to least significant. Inputs that are decimals, numeric strings, infinities, or integers outside JavaScript's safe range return an `invalid_input` error instead of being rounded or coerced.

Generate deterministic test vectors

Create exact balanced ternary digit arrays for unit tests of arithmetic, encoding, or educational software.

How much does an API conversion cost?

Each API request costs $0.002. You can also run the converter free in your browser on this page.

Read the full KIT documentation →